To clear the file path of the FileUpload control on the client

Source: Internet
Author: User
Tags file upload reset

Today in writing a project update module encountered a problem:. NET fileupload control as long as the file is selected in postback when the file will be automatically uploaded to the server, and my update module needs to have the choice of the decision to upload files, the principle is simpler to say, Directly to the FileUpload control selected files to remove the shell, but the use of JS removal of the time to find IE FileUpload control value incredibly or only read, and in Firefox can be changed. If you are dealing with a. NET commit event to save this file, it means that the file selected by the FileUpload control will be uploaded once to the server, but it is a problem you are saving, which is quite wasteful of bandwidth and server resources. But fortunately, steamed bun Blog In the solution, by comparing personal feeling the following way is better:

Reference

Create a new form, put the upload control temporarily, and then call the form reset method, finished and then put the upload control back. This form does not need to go into the DOM structure to work properly, so don't worry about having any effect on the interface.

Specific resolution code: View Plaincopy to Clipboardprint?

Put this JS into the <body> of the page
<script language= "javascript" type= "Text/javascript" >
//Empty file upload box, File for upload form pairs like
function clearfileinput (file) {
var form=document.createelement (' form ');
Document.body.appendChild (form);
//Remember the location of the file in the old form
var pos=file.nextsibling;
Form.appendchild (file);
Form.reset ();
Pos.parentNode.insertBefore (File,pos); 
document.body.removeChild (form);
}
</script>
Put this JS into the <body> of the page
<script language= "javascript" type= "Text/javascript" >
//Empty file upload box, File for upload form pairs like
function clearfileinput (file) {
var form=document.createelement (' form ');
Document.body.appendChild (form);
Remember the location of the file in the old form
var pos=file.nextsibling;
Form.appendchild (file);
Form.reset ();
Pos.parentNode.insertBefore (File,pos);
Document.body.removeChild (form);
}
</script>view plaincopy to Clipboardprint?

The client script event that adds the button, of course you can also write in the button's attribute, or add the onclick event directly on the HTML button.

Btncannel.onclientclick = "Clearfileinput (document.getElementById ('" + Upload control Id.clientid + "));"

Related Article

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.