Click here to Skip to main content
15,867,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First I choose a file using fileUpload control then I clicked a submit button to uploade the file in a database.Then the file is Uploaded. But the problem is after uploading the file if I refresh the page using F5 key the same file is again uploaded in the database.How Can I prevent this? and also How To clear a File which is just uploaded from FileUploader control when submit button is clicked. I am Using C# language in asp.net.
Posted

you should try to avoid the postback of the dropdownlist if possible. otherwise.. save the selected file in Session or ViewState to get it back at the time of submitting the page as:

C#
protected void Button1_Click(object sender, EventArgs e)
{
String savePath = @"c:\temp\";
if (FileUpload1.HasFile == true )
{
  String fileName = FileUpload1.FileName.ToString();
  savePath += fileName;
  FileUpload1.SaveAs(savePath);
}
TextBox1.Text=FileUpload1.PostedFile.FileName.ToString();
}
 
Share this answer
 
The problem is due to how browsers work. The first time you POST the page with a file upload, the browser sends all the data, including the file, then returns back HTML to render. If you hit reload (F5) then the browser resends the last POST request meaning resends the file.

In this case it tries to upload a file again, but in other cases such as billign someone or adding a record this can cause duplication. A record could be entered twice or even worse a user charged payment twice.

The way to stop this is to use a pattern called the PRG pattern. The post-redirect-get patter, where after an http post, instead of showing html, you redirect back to a page (either teh same or another).

REF :

file upload .hasfile holds true after post back!!
block / disabled referesh funtion F5 key
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-May-12 17:30pm    
My 5.
--SA
Monjurul Habib 9-May-12 1:40am    
thank you :)
Wonde Tadesse 8-May-12 17:55pm    
5+
Monjurul Habib 9-May-12 1:40am    
thank you :)
Sandeep Mewara 9-May-12 2:50am    
Perfect. 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900