Upload File With Ajax XmlHttpRequest
Hi i am trying to send file with xmlhttprequest with this code.
but i got this error : the request was rejected because no multipart boundary was found
help me pls..
- There is no such thing as
xhr.file = file;; the file object is not supposed to be attached this way.
xhr.send(file)doesn’t send the file. You have to use the
FormDataobject to wrap the file into a
multipart/form-datapost data object:
var formData = new FormData(); formData.append("thefile", file); xhr.send(formData);
After that, the file can be access in
$_FILES['thefile'] (if you are using PHP).
EDIT: The (2) above was incorrect. It does send the file, but it would send it as raw post data. That means you would have to parse it yourself on the server (and it’s often not possible, depend on server configuration). Read how to get raw post data in PHP here.
- Database Administration Tutorials
- Programming Tutorials & IT News
- Linux & DevOps World
- Ebook Reviews
- PES Matches, Skills & News