Assuming I have a file data.txt and I want to send it using POST with XHR.
I found this. All my google searches have led to the use of FormData object.
<input type="file" id="uploadfile" name="uploadfile" />
<input type="button" value="upload" onclick="upload()" />
<script>
var client = new XMLHttpRequest();
function upload()
{
var file = document.getElementById("uploadfile");
/* Create a FormData instance */
var formData = new FormData();
/* Add the file */
formData.append("upload", file.files[0]);
client.open("post", "/upload", true);
client.setRequestHeader("Content-Type", "multipart/form-data");
client.send(formData); /* Send to server */
}
/* Check the response status */
client.onreadystatechange = function()
{
if (client.readyState == 4 && client.status == 200)
{
alert(client.statusText);
}
}
</script>
This example looks nice however I want the file to be uploaded without the pick of it which isn't the case in this code.
So I thought about putting a custom value in the file input like this :
<input value = "data.txt" type="file" id="uploadfile" name="uploadfile" />
But apparently we just can't do it.
So how can I achieve this with or without using FormData knowing that I want to send this file without picking it ?