The Ajaxfileupload.js component really works, but the return value format is disgusting.
Let someone define a datatype: "JSON", and finally give back a bunch of HTML, what does that mean. May be a humble, simply modify the source code of the processing function, manual return JSON got.
To modify the code:
About 185 lines in Ajaxfileupload.js, there's a function: Uploadhttpdata
Original code vs. changed code:
Uploadhttpdata:function (r, type) {
/** original code
var data =!type;
data = Type = = "xml" | | Data? R.responsexml:r.responsetext;
If the type is ' script ', eval it in global context
if (type = = "Script")
jquery.globaleval (data);
Get the JavaScript object, if JSON is used.
if (type = = "json")
eval ("data =" + data);
Evaluate scripts within HTML
if (type = = "html")
jQuery ("<div>"). HTML (data). Evalscripts ();
return data;
*
///Change code
var data =r.responsetext;
var start = Data.indexof ("{");
var end = Data.indexof ("}");
var jsonstr = data.substring (start,end+1);
Return (jsonstr instanceof Object)? Jsonstr:eval ("(+ Jsonstr +)");
is used to process the lower return value, the value returned by this function is passed directly to the success callback function JSON value of our file upload. Directly when the object is used is OK.
Send the file upload code with the attached.
/**
* Ajax File Upload * *
("Body"). On ("Change", "input[name= ' image ')", function () {
var url = $ (this). attr (" Lang ");
$ (". Error"). Ajaxstart (function () {
$ (this). Show ();
}). Ajaxcomplete (function () {
$ (this). Hide ();
});
$.ajaxfileupload ({
url:url,
secureuri:false,
fileelementid: ' ImageFile ',
success:function ( JSON) {
if (0==json.error) {
$ ("Img[name= ' ImageFile ']"). attr ("src", Json.url). Show ();
else{
$.fn.error (json.message);
}
,
error:function (data,status,e) {
alert (e);
}
});
});
Server-side spring MVC, just return to map. Post the code as follows:
/**
* System diagram: Part of the contents of the system content map stored in this
* @param image
* @return
/@RequestMapping (value= "/system", Method=requestmethod.post)
@ResponseBody public
map<string,object> System (multipartfile image) { Return
this.upload (image,settingenum.image_system_folder,null);
}
Specific upload details will not repeat, a lot of online, there are questions please leave a message.