I am looking for something like this, but using javascript. I want to download the file into javascript memory and then add the file content to a file input so that I can upload that again. I have written a function to access the file.
function fileDownload(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.send();
xhr.onload = function() {
if (xhr.status != 200) { // analyze HTTP status of the response
console.log(`Error ${xhr.status}: ${xhr.statusText}`); // e.g. 404: Not Found
} else { // show the result
callback(xhr.response)
}
};
}
So xhr supports the response type as text, arraybuffer, blob and document. So which type should I use? I want to support all types of files. Can I use blob?In that case how can I assign it to an input type of file. Please help.