Cannot Delete File After File Upload (ASP.net 2.0)

L

LisaBigJax

I am utilizing the ASP.net FileUpload control to upload a file. After
upload, if a user tries to delete the file, I get the dreaded

The process cannot access the file
"D:\Inetpub\wwwroot\images\uploads\uploadedFile.jpg' because it is
being used by another process.

If I wait 15 seconds, the error will dissappear.

I did an extensive search on this error as it pertains to file uploads
and it seems the resolution is to close and/or dispose the file after
upload. I tried to do this with the Dispose() method after upload.

However, the error is still occuring ... any ideas? Code snippets are
below. (Using VWD,VB / 2.0)

================

Protected Sub btnUploadPic_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim fu As FileUpload = CType(DetailsView1.FindControl("FileUpload1"),
FileUpload)

checkForValidFile() // My own function to validate file

fu.SaveAs(pathPath)
fu.Dispose()

End Sub

Protected Sub btnDeleteFile_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
filePath = "d:/inetpub/wwwroot/images/uploads/fileJustUploaded.gif""
System.IO.File.Delete(filePath)
End Sub
 
R

Rajesh CKR

Are you storing any references in the validation function and not closing it
completely?

Try calling GC.Collect() after dispose and see if the error still occurs.

Also, if you are using any COM objects using CCWs, use
System.Runtime.InteropServices.Marshal.ReleaseComObject

Raj
 
L

LisaBigJax

No, I'm not using COM; I'm using the File Upload control that comes
standard with .NET 2.0 - Ill try yuor suggestion.
 
L

LisaBigJax

I am still getting this problem even after implementing the advice
above.

Anyone else? I can't seem to get this one figured out.
 
Joined
Aug 15, 2006
Messages
1
Reaction score
0
Same problem

Hello !

I am facing the exact same problem.
Could you please tell me what solution you have found ?
Thanks you very much.
 
Joined
Oct 23, 2006
Messages
1
Reaction score
0
I was having the same problem and this is how I solved it:

LisaBigJax was doing:
fu.SaveAs(pathPath)
fu.Dispose()

disposing the file upload object is not the right thing to do. It is the uploaded file that needs to be disposed.

This is what I am doing:
fu.PostedFile.SaveAs(path + FileUploader.FileName);
fu.PostedFile.InputStream.Dispose();

I hope this helps,
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top