Use the ajaxfileupload plug-in to upload non-refreshing files

Source: Internet
Author: User
This problem occurs during project creation. when the NETFileUpload control implements file upload, the page will be refreshed, and the elements spelled out by JS on the page will disappear. to upload files, the page cannot be refreshed, the ajaxfileupload plug-in is a good choice (under the plug-in... syntaxHighlighter. al

This problem occurs during project creation. NET FileUpload control implements file upload, and the page will be refreshed, so the elements spelled out by JS on the page will disappear. In order to upload files, the page cannot be refreshed, ajaxfileupload plug-in is a good choice (plug-in: http://www.phpletter.com/DOWNLOAD)

Ajaxfileupload is a plug-in of jQuery. When using this plug-in, you must reference the jQuery. js file.


Run the Code directly.

 


JS Code


[Javascript]
// Execute AJAX to upload a file
$. AjaxFileUpload ({
Url: '/Web/Teacher/ImportAchievements. ashx ',
Secureuri: false,
FileElementId: 'fullevements ',
DataType: 'json ',
Success: function (data, status ){
Alert (data [0]);
}
});

// Execute AJAX to upload a file
$. AjaxFileUpload ({
Url: '/Web/Teacher/ImportAchievements. ashx ',
Secureuri: false,
FileElementId: 'fullevements ',
DataType: 'json ',
Success: function (data, status ){
Alert (data [0]);
}
});
Note:

1. This method is very similar to the well-known $. ajax method.

2. parameter description

Url: the AJAX background code file. It must receive file data from the front-end.

Secureuri: whether to encrypt the uploaded file

FileElementId: in HTMLThe Id value of the upload control. Note that the background code receives data in the form of name-value. Therefore, the background Code uses name to receive data, instead of Id (the root cause is that this method will automatically generate a form and submit the form to the background code for processing ).

DataType: data type, generally 'json'

Success: callback function executed after the upload is successful

 


Code in ASP. NET general processing programs


[Csharp]
Public void ProcessRequest (HttpContext context ){
Context. Response. ContentType = "text/html"; // This is critical. Although the foreground data type is json, you must write html
// Obtain the file from the front-end
HttpFileCollection files = HttpContext. Current. Request. Files;
// Save the file in the website directory
Files [0]. SaveAs (context. Server. MapPath ("/Web/uploadFiles/Achievements.xls "));
// Return the message in json format
String result = "[" + "\" "+" score imported successfully "+" \ "" + "]";
Context. Response. Write (result );

}

Public void ProcessRequest (HttpContext context ){
Context. Response. ContentType = "text/html"; // This is critical. Although the foreground data type is json, you must write html
// Obtain the file from the front-end
HttpFileCollection files = HttpContext. Current. Request. Files;
// Save the file in the website directory
Files [0]. SaveAs (context. Server. MapPath ("/Web/uploadFiles/Achievements.xls "));
// Return the message in json format
String result = "[" + "\" "+" score imported successfully "+" \ "" + "]";
Context. Response. Write (result );

}
In this way, AJAX is implemented to upload files, and the page will not be refreshed. Please try it if necessary.

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.