ajaxFileUpload.js a lot of the same name, because it is a very easy to do.

I use is this: https: //github.com/carlcarl/AjaxFileUpload

Download here: http: //xiazai.jb51.net/201701/yuanma/ajaxfileupload ( jb51.net) .rar

AjaxFileUpload.js is not a very well-known plug-ins, but others written put out for everyone to use, the principle is to create a hidden form with JS and iframe then go submit, get a return value.

had made a asynchronous upload function, choose to configure it because it’s way more like the jQuery AJAX, I love it.

comment which comes not do. That’s because we are not using the same js. I found on github AjaxFileUpload out many similar js.

ajaxFileUpload is a jQuery plugin asynchronous upload files.

pass a version of do not know what came later do not find everywhere.

syntax:. $ ajaxFileUpload ([options])

options Parameters:

1, url upload handler address.
2, fileElementId uploaded file domain ID, a ‘s ID.
3, secureuri whether security is enabled submission, the default is false.
4, dataType data type returned by the server. It can be xml, script, json, html. If you leave, jQuery will automatically determine.
5, success submission processing function automatically performed after a successfulNumber, the parameter DATA is the data returned by the server.
6, ERROR submit the processing function of the failed automatic execution.
7, DATA custom parameters. This thing is more useful, when there is data related to the uploaded picture, this thing is used.
8, TYPE When you want to submit a custom parameter, this parameter is set to POST

Error Tip:

1, SyntaxError: missing; Before Statement error
If this error needs to be checked if the URL path can be accessed

If this error occurs, it is necessary to check whether the server background processing program for processing submission operations is. Presence syntax error

If this error occurs, you need to check if the text domain property ID exists

4, SyntaxError: Missing } in XML Expression Error
If this error occurs, check if the file name is unanimous or does not exist

5. Other custom error
Method for printing directly using variable $ error Check if each parameter is correct, it is more convenient than these invalid error tips.


Step 1: First introduce jQuery and AjaxFileUPload plugin. Be careful in the order, this is not to say, all plugins are like this.

      HTML code:  


A third step: JS code


$ (“: button”). Click (function () {ajaxfileupload ();})} () {$ .ajaxfileupload ({URL: ‘/upload.aspx ({url:’ /upload.aspx ‘, // Upload server-up request address secureuri: false, // Do you need a security protocol, usually set to false fileElementid:’ file1 ‘, // file uploaded domain ID DATATYPE:’ JSON ‘, // Return The value type is typically set to JSON Success: function (data, status) // server success response handler {$ (“# img1”). Attr (“src”, data.imgurl); if (Typeof (data.error)! = ‘undefined’) {if (data.error! = ”) {alert (data.error);} else {alrt (data.msg);}}}, error: function (data, status, e) //// Server Response Failure Processing Function {Alert (E);}} Return FalsE;}

Step 4: Backstage page UPLOAD.ASPX code:
     protected void Page_Load (Object sender, eventargs e) {httpfilecollection files = request.files; string msg = string.empty; string error; if (files.count> 0) {files [0] .saveas. Mappath ("/") + system.io.path.getFileName (files [0] .filename); msg = "Success! File size is:" + files [0] .contentLength; imgurl = "/" + files [ 0] .filename; String res = "{error: '" + error + ", msg:'" + msg + ", imgurl: '" + imgurl + "}"; response.write (res); response    This example complete code download  
Controller code

Public class homecontroller: controller {public actionResult index () {returnireView ();} PUBLICActionResult Upload () {HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files; string imgPath = “”; if (hfc.Count> 0) {imgPath = “/ testUpload” + hfc [0] .FileName; string PhysicalPath (IMGPATH); HFC [0] .saveas (PhysicalPath);} return const (imgpath);}}
front end view, HTML and JS code After successful upload, return the image real address and bound to the SRC address

$ (“: button”). Click (“# file1”). Val (). Length> 0) {ajaxfileupload ();} else {Alert (“Please select a picture”);}})}) function ajaxfileupload () {$ .ajaxfileupload ({URL: ‘/ home / upload “, // Used to file upload server-side request address secureuri: false, / / Generally set to false fileElementid: ‘file1’, // file upload id attribute
DataType: ‘html’, // Return value type generally set to jsOn Success: Function (DATA, STATUS) // Server Success Response Processing Function {Alert (DATA); $ (“# IMG1”). Attr (“SRC”, DATA); IF (TypeOf (Data.Error)! = ‘ Undefined ‘) {if (data.error! =’) {Alert (data.error);} else {Alert (data.msg);}}}, error: function (data, status, e) // server response Failure processing function {Alert (e);}}) Return False;}

        Last one will come back to an upload picture and an example of the parameter: controller code:      Public Class HomeController : Controller {public ActionResult Index () {return View ();} public ActionResult Upload () {NameValueCollection nvc = System.Web.HttpContext.Current.Request.Form; HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files String imgpath = ""; if (hfc.count> 0) {imgpath = "/ testupload" + HFC [0] .filename; string physicalPath = server.mappath (imgpath); HFC [0] .saveas (PhysicalPath);} // Note To write the second third Parameter returnjs (new {id = nvc.get ("id"), name = nvc.get ("name"), IMGPATH2 = IMGPATH}, "text / html", jsonrequestbehavior.allowget);}} 


$ (function () {$ (“: butt () {if ($” “# file1″). Val (). Length> 0) {ajaxfileupload ();} else {Alert (” Please select a picture “);}})}” Function ajaxfileupload () {$ .ajaxfileupload ({URL: ‘/ home / upload “, // Used server-side server-up request address type:’ post ‘, data: { ID: ‘123’, name: ‘lunis’}, // This parameter is very rigorous. If a quotation number is not line, you will be set to false fileElementid: ‘file1’, // File upload space ID attribute
DataType:’json’, // Return value type Generally set to JSON Success: function (data, status) // server success response processing function {Alert (data); $ (“# img1”). Attr (“src”, data. IMGPATH2); Alert (“Your request is” + Data.ID + “” + “Your request name is:” + data.Name); if (TypeOf (data.error)! = ‘undefined’) {IF (DATA.ERROR! = ‘) {Alert (data.error);} else {alrt (data.msg);}}}, error: function (data, status, e) // server response failed handler {Alert (e);}}) Return False;}


This example is displayed while displaying an asynchronous uploading image and pops up a parameter of a self-defined transmission. This example download address
On January 28, 2013, a problem is found during the debugging process, that is, as a file domain (
) must have a name property. If there is no Name property, the server is acquired after uploading No picture. Such as: The correct way is    January 28, 2013, the most classic mistake finally found the reason. Object Function (A, B) {Return New E.FN.INIT (A, B, H)} HAS No Method 'HandleError', this is an error in Google Browser, veryClassic, I don't know if it is my version of the problem, or there is a problem. The root of this problem has been found to find the problem in N times uploaded. The answer is: DataType parameter must be capitalized. Such as: DataType: 'html'.   2016-07-28, a commentary in the comment: TypeError: $ .ajaxfileupload is not a function We are not the same JS, you have n'thing AjaxFileUpload.   The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Cloud.                      
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment