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

Source: Internet
Author: User

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.

 

 

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.