Uploading both data and files in one form using Ajax?

后端 未结 10 831
无人共我
无人共我 2020-11-21 05:48

I\'m using jQuery and Ajax for my forms to submit data and files but I\'m not sure how to send both data and files in one form?

I currently do almost the same with b

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-21 06:44

    In my case I had to make a POST request, which had information sent through the header, and also a file sent using a FormData object.

    I made it work using a combination of some of the answers here, so basically what ended up working was having this five lines in my Ajax request:

     contentType: "application/octet-stream",
     enctype: 'multipart/form-data',
     contentType: false,
     processData: false,
     data: formData,
    

    Where formData was a variable created like this:

     var file = document.getElementById('uploadedFile').files[0];
     var form = $('form')[0];
     var formData = new FormData(form);
     formData.append("File", file);
    

提交回复
热议问题