Ajaxfileupload File Upload return value processing Ajaxfileupload.js + spring MVC file Upload __js

Source: Internet
Author: User
Tags file upload

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.