Reputation: 39
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
Upvotes: 1
Views: 16421
Reputation: 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
Upvotes: 1