JavaScript - New File() not working

后端 未结 1 436
栀梦
栀梦 2021-01-07 11:31

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

var txtfile =\"../wp-content/plugins/MedicAdvisor/test.txt\";
             


        
相关标签:
1条回答
  • 2021-01-07 12:04

    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

    0 讨论(0)
提交回复
热议问题