1

I am trying to write some strings to a file using JavaScript through the following code

var txtfile ="../wp-content/plugins/MedicAdvisor/test.txt";
    var file = new File("hello",txtfile);
    //file = fopen("", 3);// opens the file for writing
    file.open("w");

    var currentrow = 0;
    var nextrow = 0;
    var type = " ";
    var noofrows = 0;
    var noofcells = 0;
    var contentarray;
    var row = document.getElementsByTagName('tr');
    //get all elements having input tag
    var inp = document.getElementsByTagName('input');
    // traverse through all input tags
    for (var i=2; i<inp.length; i++){
        // see if it is a heckbox
        if(inp[i].type == "checkbox"){
            // see if it is checked
            if(inp[i].checked == true){
                //index of current row
                currentrow = inp[i].parentNode.parentNode.rowIndex;
                //event type
                type = inp[i].parentNode.parentNode.cells[6].innerHTML.trim();
                if (type == "cycling_road_race"){
                    noofrows = 6;

                    for(var j=0; j<noofrows; j++){
                        noofcells = row[currentrow + j + 1].cells.length;
                        for (var k=1; k<noofcells; k++){
                            //alert (row[currentrow + j + 1].cells[k].innerHTML.replace('<br>' , ' '));
                            contentarray.push(row[currentrow + j + 1].cells[k].innerHTML.replace('<br>' , ' '));
                            file.writeln(row[currentrow + j + 1].cells[k].innerHTML.replace('<br>' , ' '));
                        }
                    }
                }
                else if (type == "cycling_criterium_or_circuit_race"){
                    noofrows = 6;
                }else if (type == "cycling_cyclocross"){
                    noofrows = 6;
                }else if (type == "running_race"){
                    noofrows = 6;
                }else if (type == "rugby_football_hockey"){
                    noofrows = 6;
                }else if (type == "music_festival"){
                    noofrows = 6;
                }else if (type == "manual_selection"){
                    noofrows = 5;
                }
            }
        }
    }

but I am getting following error when I try to execute this code

Failed to construct 'File': The 1st argument is neither an array, nor does it have indexed properties

Kindly help me resolve this issue

GG.
  • 21,083
  • 14
  • 84
  • 130
Faizan Zahid
  • 39
  • 1
  • 3
  • 7
  • 1
    It looks to me like you want to write the file on the webserver. This is not possible with javascript, because Javascript is only run in the viewers browser and has no direct server filesystem access. – Seb Dec 13 '16 at 08:28
  • 1
    If you want to use the JavaScript File class then you are not using the constructor correctly (see [implementation notes on MDN](https://developer.mozilla.org/en/docs/Web/API/File#Implementation_notes)). However as @Seb pointed out, you seem to be trying to do something that is not supported on browsers – UnholySheep Dec 13 '16 at 08:31
  • In other words, what you are trying to do is only possible via backend/server-side code. Javascript is frontend. – John Evans Solachuk Dec 13 '16 at 08:34
  • What I am trying to do is .. export the contents of html table to txt file .. Kindly help me how to do this – Faizan Zahid Dec 13 '16 at 08:37
  • Are you trying to save data at a server? – guest271314 Dec 13 '16 at 08:42

1 Answers1

1

As the error message indicated, File constructor expects an array as first parameter. Also the second parameter should only be the file name and extension. You can also set type as a valid MIME type and lastModified as properties of object at third parameter to File constructor.

var txtfile = "test.txt";
var file = new File(["hello"], txtfile
           , {type:"text/plain", lastModified: new Date().getTime()});

File.prototype does not have an .open method. You can use File.prototype.slice() to create a new File object and concatenate data new data to the previously created File object.

file = new File([file.slice(0, file.length), /* add content here */], file.name);

Saving a File object to server requires posting the File object to server to read the contents of the file data.

var request = new XMLHttpRequest();
request.open("POST", "/path/to/server");
request.send(file);

where file contents can be read at php using php://input

$input = fopen("php://input", "rb");

See Trying to Pass ToDataURL with over 524288 bytes Using Input Type Text

Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
  • @FaizanZahid If you are trying to create, edit, save and a file at local filesystem see [Edit, save, self-modifying HTML document; format generated HTML, JavaScript](http://stackoverflow.com/questions/30563157/edit-save-self-modifying-html-document-format-generated-html-javascript) – guest271314 Dec 13 '16 at 09:12