Home > Back-end >  See a lot of posts in an ajax asynchronous upload file to set the contentType to false, but the back
See a lot of posts in an ajax asynchronous upload file to set the contentType to false, but the back

Time:04-20

The front-end code:
The function uploadFiles () {
Var accessoryID=document. GetElementById (' accessoryID). The value;
Var param="msgID='& amp; AccessoryID="+ accessoryID;
Var fileData=https://bbs.csdn.net/topics/document.getElementById (' uploadFile.) files [0].
Var formData=https://bbs.csdn.net/topics/new formData ();
FormData. Enctype="multipart/form - the data";
FormData. Append (' uploadFile fileData);
The console. The log (formData. Get (' uploadFile '));
$. Ajax ({
Url: "& lt; % request. The getContextPath (); % & gt;/uploadgongGaoAccessory struts?" + param,//transport address
Type: "post",
Data: formData,//transport data
ProcessData: false,
ContentType: false,
Cache: false,
//success callback function
Success: the function (result) {

},
//callback function failure
Error: function (errorObj) {
//errorObj returned an error encapsulation of js object
//the error object to print to the browser console
The console. The log (errorObj);
//give a friendly reminder
Alert (" service busy,,, ");
}
});
}



The backend error:
Org.apache.com mons. Fileupload. FileUploadBase $InvalidContentTypeException: the request doesn 't contain a multipart/form - data or multipart/mixed stream, the content type, the header is false

CodePudding user response:

The function the import () {
Var formData=https://bbs.csdn.net/topics/new formData ();

FormData. Append (" file ", $(' # 'file') [0]. Files [0]).
$. Ajax ({
Url: '/API/v1/XXX, XXX,
DataType: 'json,
Type: "POST",
Async: false,
Data: formData,
ProcessData: false,//the data do not handle
ContentType: false,//don't set the content-type request header
Success: the function (data) {

}
});
}
The controller
@ PostMapping (value="https://bbs.csdn.net/topics/import", produces="application/json; Charset=utf-8 ")
Public String import (@ RequestParam (" file ") MultipartFile MultipartFile) {
Return xxxService. Import (multipartFile);
}

Hope to adopt

CodePudding user response:

This is the back-end SSH framework when parsing the file is
Public ActionForward execute (ActionMapping mapping, actionforms form,
It request, HttpServletResponse response)
{

DiskFileUpload fu=new DiskFileUpload ();
FileItems=fu. ParseRequest (request);//this line the error said the request doesn 't contain a multipart/form - the data or multipart/mixed stream, content
Type the header is false
return null;
}

The front-end ajax set the contentType: false,
  • Related