A small requirement for recent projects is to upload an Excel file in the form form and then read the uploaded Excel data in controller and then perform the DAO operation
The front end code is as follows:
<div class= "Apply-r" > <a class= "Pure-button upload" style= "Text-decoration:none" href= "
#" onclick= "$ (' # Upid '). Click (); "> Import commodities </a> <input id=" upid "style=" Display:none; "class=" Up-file "
type=" file "Name=" Files "onchange=" Filechange (this); " />
</div>
JS Code:
Bulk upload file
function Filechange (target) {
if (target.files && target.files[0]) {
var file = Target.files[0];
Target.value = "";
var filedata = new FormData ();
Filedata.append ("Multipartfile", file);
Doupload (filedata);
}
function Doupload (filedata) {
console.log ("Doupload");
$.ajax ({
URL: ' #springUrl ("/product/addbatch.do") ',
type: ' POST ',
data:filedata,
async:false ,
Cache:false,
contenttype:false,
processdata:false,
success:function (result, status) {
if (Result.statuscode = =) {
$ (". Session-model"). Show ();
return;
}
if (status = = "Success" && result.success) {
showtips (result.message? Result.message: Result.returnmessage);
ProductList (1)
} else {
showtips (result.message? result.message:result.returnMessage);
}
}
) ;
}
Background Java code: Part of the code is not posted, here we just carry on the description and introduction of Content-type
@RequestMapping (value= "/addbatch.do")
@ResponseBody public
ajaxresponse Addbatch (HttpSession session, @ Requestparam ("Multipartfile") Multipartfile multipartfile) {
Ajaxresponse ajaxresponse = new AjaxResponse ();
try {
String Merchantid = Processsessionuser.getmember (session, commonconstant.member_id);
Read the data from the imported document
list<productinfo> productinfolist = Exceltoobjectconvert.excelconverttodomain ( Multipartfile, Merchantid);
The operation to insert into the database
Productmanageservice.addbatch (productinfolist);
Processajaxresponse.processresponse (True, ajaxresponse, NULL, returncode.add_success);
catch (Exception e) {
logger.error ("Execute [addbatch] Exception:", e);
Exceptionconvert.convertexception (Ajaxresponse, E);
}
Logger.info ("Return web result: {}", ajaxresponse);
return ajaxresponse;
}
The above front-end and back-end code executes correctly on Google IE11, but there are compatibility issues on the IE9.
The front-end colleague then modifies the code in the following ways:
<div class= "Apply-r" >
<label> <a class= "Pure-button upload" style= "Text-decoration:none"
> Import Products </a>
<form id= "MyForm" method= "post" enctype= "Multipart/form-data" >
<input type= " File "id=" Upid "name=" Multipartfile "onchange=" Filechange (This) "style=" Display:none "/>
</form>
</label>
</div>
Front-End JS modified as follows:
Bulk upload file
function Filechange (target) {
var option = {
URL: ' #springUrl ('/product/addbatch.do ') ',
Type: ' POST ',
success:function (data) {
var objdata = {};
if (Data.indexof (' <pre> ') >=0) {
objdata = Json.parse ($ (data). HTML ()); else{
objdata = Json.parse (data);
}
if (Objdata.statuscode = =) {
$ (". Session-model"). Show ();
return;
}
if (objdata.success) {
showtips (objdata.message? objData.message:objData.returnMessage);
ProductList (1)
} else {
showtips (objdata.message? objData.message:objData.returnMessage);
}
} ,
error:function (data) {
//alert (data.)
},
resetform:true
};
$ ("#myForm"). Ajaxsubmit (option);
return false;
}
At this point in the case of IE9, the front-end colleague asked me to return a text-formatted string, but the string's contents are in a JSON format
Before the postman debugging, the background code can be modified before the action returned to see the Content-type as Application/json;charset=utf-8
So this time we're going to modify the Content-type returned by the backend code into text format.
You can read the article I loaded. Content-type in HTTP request and its application in spring MVC
This article describes in detail the SPRINGMVC under Content-type, which focuses on the requestmapping of some of the value of the parameters
Consumes: Specifies the submitted content type (CONTENT-TYPE) of the processing request, such as Application/json, text/html;
Produces: Specifies the type of content returned, which is returned only if the specified type is included in the (Accept) type in the request header
Of course, about how spring returns to processing JSON or XML format data can be used to matter, here we first say this method, in the previous article can be seen to modify the text format to Content-type to Text/plain
Common media format types are as follows: text/html:html format text/plain: Plain text Format text/xml:xml format image/gif:gif picture format image /jpeg:jpg picture format image/png:png picture format
So I modified the background code as follows:
@RequestMapping (value= "/addbatch.do", produces= "Text/plain;charset=utf-8")
@ResponseBody public
String Addbatch (HttpSession session, @RequestParam ("Multipartfile") Multipartfile multipartfile) {
ajaxresponse Ajaxresponse = new Ajaxresponse ();
try {
String Merchantid = Processsessionuser.getmember (session, commonconstant.member_id);
list<productinfo> productinfolist = Exceltoobjectconvert.excelconverttodomain (MultipartFile, merchantId);
Productmanageservice.addbatch (productinfolist);
Processajaxresponse.processresponse (True, ajaxresponse, NULL, returncode.add_success);
catch (Exception e) {
logger.error ("Execute [addbatch] Exception:", e);
Exceptionconvert.convertexception (Ajaxresponse, E);
}
Logger.info ("Return web result: {}", ajaxresponse);
Return json.tojsonstring (Ajaxresponse);
}
Produces processing is the return value of the data format if you set produces= "Text/plain" so that the returned data is garbled
So to set the Charset=utf-8
We can view it under the postman.