Skip to content Skip to sidebar Skip to footer

Html File Save - How To Overcome The 2k Limit On Get. (note Post Does Not Work)

I am saving a file constructed in javascript to the local internet downloads directory. This works: javascript var filename = 'mysave.txt' var contents = 'xyz'; document.location =

Solution 1:

php is not necessary to create, prompt user to save file created at javascript. You can use <a> element with download attribute set to filename, href set to objectURL of Blob containing file data to initiate prompt for user to save file.

If you want to use php you can utilize php://input, file_get_contents(), echoPOSTed Blob or File object.

var i;
var filename = "mysave.txt"var contents = "";
for(i=0, contents = "";i<10000;i++){
    contents += "x";
}

var data = newBlob([contents], {type:"text/plain"});
var file = URL.createObjectURL(data);
var a = document.createElement("a");
a.download = filename;
a.href = file;
a.click();

Using file_ge_contents(), php://input

javascript

var i;
var filename = "mysave.txt"var contents = "";
for(i=0, contents = "";i<10000;i++){
    contents += "x";
}

var data = newBlob([contents], {type:"text/plain"});

var request = newXMLHttpRequest();
request.open("POST", "/path/to/server");
request.setRequestHeader("x-file-name", filename);
request.reponseType = "blob";
request.onload = function() {
  console.log(this.response); // `echo`ed file
};
request.send(data);

php

echo file_get_contents("php://input");

See also Generate png-file with php from dataURI sent through ajax ; though note request.setRequestHeader("x-file-name", filename); should have been included at javascript portion of Answer for $tmpFilename = $_SERVER["HTTP_X_FILE_NAME"]; at php

Post a Comment for "Html File Save - How To Overcome The 2k Limit On Get. (note Post Does Not Work)"