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.