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.
Go directlyCodeRight
JS Code
// Execute ajax to upload a file $. ajaxfileupload ({URL: '/web/teacher/importachievements. ashx', secureuri: false, fileelementid: 'fullevements', ype: '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: <input type = "file"/> specifies the id value of the upload control in HTML. Note that the background code receives data in the form of name-value, therefore, the background Code uses the name to receive data, rather than the 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
ASP. NETProgramCode in
Public void processrequest (httpcontext context) {context. response. contenttype = "text/html"; // This is critical. Although the foreground data type is JSON, you must write HTML // to obtain the file 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 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.