I am trying to write some strings to a file using JavaScript through the following code
var txtfile =\"../wp-content/plugins/MedicAdvisor/test.txt\";
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