www.webdeveloper.com
Results 1 to 4 of 4

Thread: How to Load an Array from a text file in JavaScript?

  1. #1
    Join Date
    Dec 2013
    Posts
    1

    How to Load an Array from a text file in JavaScript?

    I am a complete Beginner using Javascripts and simple descriptive solutions would be very helpful. This is completely on one machine, no server available and I just need to do basic stuff using HTML etc.

    I have a task wherein I need to provide an option on the browser to Upload a text file (containing numbers separated by a comma) using Mozilla Firefox only, read the uploaded file into an array, perform some computation on that array and then store the array in another text file and provide an option on the web browser to download the output file.

    This is what I have done so far.

    Code:
    **********************************
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <script src="javaScripts/script.js" type="text/javascript"></script>    
       <script src="script.js"></script>
    
    <head runat="server">
        <title></title>
    </head>
    <body>
    
        <input type="file" name="input" size="40"/>
        <br />
        <input id="Button" type="button" value="Average" onclick="CPU_Average()"/>
        <br />
        <span id="output1"></span>
    
    </body>
    </html>
    
    <script id="average" type="text/javascript">
    function CPU_Average() {
        var output1 = document.getElementById("output1");
        output1.innerHTML = "";
        var row = 5;
        var col = 5;
        var size = row*col;
        try{
        var windowSize = 3;
        var i=0,j=0,k=0,p=0,q=0,count=0,limit=1;
        var check=0;
        var sum=0.0;
    
        var vec1 = new Uint32Array(size);
        var vec_op = new Float32Array(size);
    
        for (i=0; i<size;  i=i+1) {
            vec1[i] = Math.floor(Math.random() * 100); //Random number 0..99
        }
         output1.innerHTML += "<br>Vector length = " + size;
        output1.innerHTML += "<br>Vector1 = ";
        for (i = 0; i < size; i = i + 1) {
            output1.innerHTML += vec1[i] + ", ";
        }
    
        for(k=0;k<size;k=k+1)
        {
            sum=0.0;
            i=Math.floor(k/row);
            j=k%row;
            count=0;
    
            for(p=i-limit;p<=i+limit;p=p+1)
            {
                for(q=j-limit;q<=j+limit;q=q+1)
                {
                    if(p>=0 && q>=0)
                    {
                        check = ((p*row)+q);
                        if(check<size && ((q*col)+p)<size)
                        {
                            sum = sum+vec1[check];
                            count=count+1;
                        }
                    }
                }
            }
    
            vec_op[k] = (sum/count);
        }
    
            output1.innerHTML += "<br>Result = ";
        for (i = 0; i < size; i = i + 1) {
            output1.innerHTML += vec_op[i].toFixed(2) + ", ";
        }
        }
        catch(e) {
            document.getElementById("output1").innerHTML
            += "<h3>ERROR:</h3><pre style=\"color:red;\">" + e.message + "</pre>";
            throw e;
        }
    }
    </script>
    ***********************
    currently I am generating random numbers and storing them in the input array(vec1), but I need to load that array from an input file. Also my output array(vec_op) prints into a text box but I need to write that array in a text file, each element separated by a comma and allow the user to download it.

    If I need to use JQuery or Ajax for it then please explain how to add them in the above code.

    Thanks.

  2. #2
    Join Date
    May 2004
    Location
    chennai, tamil nadu, India
    Posts
    437
    Maybe you can post the form to a PHP file in the server and use PHP to process the uploaded file and return back.
    Chris, Senior Developer,
    www.Chrisranjana.com

  3. #3
    Join Date
    Mar 2009
    Posts
    468
    Since you say that this is all on one machine you will nevertheless need a server to write your data to a text file. The nearest thing javascript running from a browser can do in this regard is read/write cookies or to/from localStorage. You can download free server software such as Apache and run it on your local machine. Or you can download node,js (server side javascript) with which you can create a simple server that is perfectly capable of reading and writing text files.

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,213
    The alternative if you have no server & server-side is to use the save page as option, select text files and save the computed data as a text file by having your results written as plain text in to another window.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles