How can I upload a file using AJAX without using multipart?

前端 未结 1 1193
一向
一向 2021-01-27 02:29

The only file my app allows users to upload are images, and they are always uploaded as the sole input field in a form. Thus, multipart is unnecessary, and I could much more eas

相关标签:
1条回答
  • 2021-01-27 02:31

    You can simply send the associated File or Blob itself via XHR level 2. For example, in the upload library I maintain (Fine Uploader) you can elect to have files sent in multipart encoded POST requests (all browsers) or non MPE requests (only browsers that support the File API).

    To send the file in a MPE POST request, as you may already know, you must either add your file to a FormData object and send that via XHR2, or submit a form containing a file input. If you want to upload a file in a non MPE POST request, simply do this:

    xhr.send(file);
    

    Of course, this can only be done in browsers that support the File API. Also, if you want to send any parameters along with your file, you will have to include them in the query string.

    0 讨论(0)
提交回复
热议问题