Uploading Files with Ajax XMLHttpRequest: Troubleshooting the "No Multipart Boundary Found" Error
When attempting to upload a file using XMLHttpRequest, you may encounter an error like "The request was rejected because no multipart boundary was found." This issue arises when the file is not sent correctly as a multipart/form-data request.
One common mistake is attaching the file to the XMLHttpRequest object directly, as shown in the code:
xhr.file = file; // not necessary if you create scopes like this
This method is incorrect. Instead, the file should be wrapped into a FormData object, which constructs the proper multipart/form-data request payload:
var formData = new FormData(); formData.append("thefile", file);
Once the file is added to the FormData object, you can use xhr.send(formData); to submit the request. The file will be accessible on the server-side within $_FILES['thefile'] if using PHP.
Remember that you can consult resources like MDC and Mozilla Hack demos for further guidance on file uploads with Ajax XMLHttpRequest.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3