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 + "));"