Results 1 to 4 of 4

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

  1. #1
    Join Date
    Dec 2013

    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.

    <!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">
        <input type="file" name="input" size="40"/>
        <br />
        <input id="Button" type="button" value="Average" onclick="CPU_Average()"/>
        <br />
        <span id="output1"></span>
    <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;
        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] + ", ";
                    if(p>=0 && q>=0)
                        check = ((p*row)+q);
                        if(check<size && ((q*col)+p)<size)
                            sum = sum+vec1[check];
            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) {
            += "<h3>ERROR:</h3><pre style=\"color:red;\">" + e.message + "</pre>";
            throw e;
    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.


  2. #2
    Join Date
    May 2004
    chennai, tamil nadu, India
    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,
    Php laravel developers,

  3. #3
    Join Date
    Mar 2009
    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
    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.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

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