Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Clear the hasfile Property on an asp:FileUpload Control?

I have a .NET 2.0 fileupload control, and next to it a button, like this:

<asp:fileupload id="uploadedFile" runat="server" />
<asp:Button runat="server" ID="upload" Text="Upload" OnClick="Page_Load"/>

Now, this is all on a page within a webpart (sharepoint, that is - for those of you unfamiliar with webparts - it is basically an iframe). Notice also that I am calling Page_Load.

After the user browses for the file, the hasfile property of the uploadedFile control becomes true (it was false to begin with). Fine. The user clicks the button and the file uploads. I have code in there that sends me an email to let me know a file has been uploaded as long as hasfile is true.

If the user pushes the refresh button in his browser, the page is reloaded, but the hasfile property is not cleared, and so I get sent another email! (If I use Firefox's ability to reload without the cache, then everything is refreshed correctly)

How can I clear the hasfile property on the FileUpload control? Or how can I cause the page to skip the cache when the user reloads?

I don't mind calling a function other than Page_Load, but then it needs to be able to receive a parameter so I can also call it from Page_Load (on account of other things I am doing).

like image 343
bgmCoder Avatar asked Dec 10 '22 00:12

bgmCoder


1 Answers

Add the functionality of clearing the hasfile property in the LoadPage() function which ,suprise suprise, is called when you refresh (as the page loads).

Or maybe instead of placing the functionality, code a function that implements said functionality and call it from page load. Guide Code:

private void clearFileUpload()
{
    fileUpload.Dispose();
}

OR

try to deal with the issue immediately after uploading the file.

private void clearFileUpload()
{
    fileUpload.PostedFile.InputStream.Dispose();
    // fileUpload.HasFile is now  false
}
like image 107
Lyuben Todorov Avatar answered Mar 02 '23 18:03

Lyuben Todorov