AJAX實(shí)現(xiàn)文件上傳功能報(bào)錯(cuò)Current request is not a multipart request詳解
想做一個(gè)文件上傳,spring boot配合Ajax來(lái)進(jìn)行。卻報(bào)錯(cuò):Current request is not a multipart request
這是錯(cuò)誤截圖:
當(dāng)時(shí)發(fā)生這種錯(cuò)誤,我是很震驚的,我以為找了很多辦法來(lái)解決。
有以下辦法,當(dāng)然這些并未解決我的問(wèn)題,但是部分有用,就先列舉出來(lái):
1.在頁(yè)面頭部加入信息:
<meta http-equiv="Content-Type" content="multipart/form-data; charset=utf-8" />
此方法 未解決 問(wèn)題!
2.在form表單加入屬性:enctype
<form method="post" enctype="multipart/form-data"> <input type="file" name="file" /></form>
此方法 未解決 問(wèn)題!
3.后端:@RequestParam MultipartFile file 改為 @RequestPart MultipartFile file
此方法 未解決 問(wèn)題!
還有個(gè)別方法。。。都不行
下面是解決方法,我之前的ajax代碼:
$("#upload-ok").click(function () {var form = document.getElementById("upload-form");var file = new FormData(form);$.ajax({ url: "/addFile", type: "POST", processData:false, contentType:false, data:{"file":file }, success: function (date) {xxxxxx }, error: function (date) {xxxxxx }}) })
正確代碼:
$("#upload-ok").click(function () {var form = document.getElementById("upload-form");var file = new FormData(form);$.ajax({ url: "/addFile", type: "POST", processData:false, contentType:false, //重要部分,data的傳的是整個(gè)表單,不用大括號(hào)包裹;不用自定義變量名 data:file, success: function (date) {xxxxxx }, error: function (date) {xxxxxx }}) })
就這樣,解決!!!
到此這篇關(guān)于AJAX實(shí)現(xiàn)文件上傳功能報(bào)錯(cuò)Current request is not a multipart request詳解的文章就介紹到這了,更多相關(guān)AJAX文件上傳內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
