JQuery Ajax File Upload

Source: Internet
Author: User

Used before the ajaxfileupload, but the feeling is not very easy to use, today, the front-end file on the issue of a point, simply a lunch break on their own extended a simple jQuery ajax upload file plug-ins, need to use the upload function of the page, just in the jQuery introduction, the introduction of the JS file can be. Due to the use of Formdata, the lower version of IE does not necessarily support, we are backstage management system, are used Chrome,firefox and 360 of the domestic Chrome core browser, so do not consider this issue.

Sticky code to remember: Fileupload.js

/** * jquery upload file extension * Use Method Example (optional parameters in square brackets): * $.fileupload ({* URL: ...,//File upload address *: "#uploadFile", The CSS selector Specifies the file tags to be uploaded * [filename: ...],//uploaded to the server's filename * [data:{id:1,user: "Xiaoming"}],//Custom uploaded parameters * [su
 Ccess:function (data) {Console.log (data)}]////Post successful callback function *}); * Note: This plugin IE9 and below does not support * * AUTHOR:LIXK * date:2016/11/24/var fileupload = function (params) {var file = $ (param s["File"]). Get (0) | | {}; File object var filename = params["filename"] | |  File.name; File name for upload to server, default file name var filecontent = File.files[0] | | {};  File content var url = params["url"]; Upload address var data = params["Data"] | | {}; Custom parameter var success = params["Success"]; Upload successful callback function var formData = new FormData ();
	Create Upload Form formdata.append (fileName, filecontent);
	Add parameters to the form for (var key in data) {Formdata.append (key, Data[key]); ///Send form data to server $.ajax ({url:url, type: "POST", Data:formdata, Processdata:false,//tell jquery not to handle sending data con Tenttype:False,//tell jquery not to set Content-type request header success:function (data) {//request succeeded, callback if (success) {success (data);
}
		}
	}); }//Add JQuery extension jquery.extend ({fileupload:fileupload});

Picture Upload Use Example:

$.fileupload ({
	url:uploadlogo_url,
	file: ' Input[name= ' logofile "] ', 
	//filename:" Logofile ",
	Success:function (data) {
		$ ("#logoImg"). attr ("src", data.imgpath);//preview Picture
	}
});




Related Article

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.