I've having a issue with XHR2 to post to file to server. I have the following issues:
I'd like to perform a POST to a URL, with two parameters and the FILE. I'd like the POST request to act like I used an input=file on the back-end.
The goal is have something like this:
function sendForm(FILE_URI, GO_TO) {
var formData = new FormData();
formData.append('param1', "param1");
formData.append('param2', "Param2");
// Now we add the file
formData.append("file", FILE_URI);
var xhr = new XMLHttpRequest();
xhr.open('POST', GO_TO, true);
xhr.onload = function(e) { ... };
xhr.send(formData);
return false; // Prevent page from submitting.
}
This code won't work and the server will not look at the "file" param as a $_FILE (I'm using PHP). Remember - there is no File Upload element in the form, it will just be a hidden input with the URI:
<input type="hidden" id="file_item" value="/path/to/image" />
Any guidance on this?
UPDATE
I understand that editing the INPUT file is bad - I don't want to do that. I want to find other ways to POST a file to server without a FORM. I can use XHR2 to upload a BLOB without a form - but I'm looking for ways to have Params + File (or Blob) using XHR2.