1

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">
    <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.

  • http://my.opera.com/community/forums/topic.dml?id=119040 possible match – Vivekh Dec 09 '13 at 05:20
  • @Vivekh: Saw that post earlier but I am unable to understand the arguments in it and also where to place that code in my code. Im writing a javascript code for the first time and really confused.:( – user3081567 Dec 09 '13 at 05:34
  • Try using file api. See [How do I read in a local text file with javascript?](http://stackoverflow.com/questions/9756411/how-do-i-read-in-a-local-text-file-with-javascript) – Anto Jurković Dec 09 '13 at 09:42

0 Answers0