0

I am developing a website where user can upload images. I am saving images into a FTP folder (not in db) and the details of it in the database (file name, path, etc).

I have noticed that when working on localhost the following code works just fine, it doesn't give me any error. But when uploaded to server(with windows hosting plan), it doesn't delete the file and gives me error that: cannot delete the file because it is used by another process.

I have made sure that the directory has suitable file permissions but still it does not delete it. and strange enough, when i use FTP client to delete it manually, it locks the file and doesn't allow me to delete from there. I have to recycle the app pool to do so.

I am looking for best way to delete file from windows based server when you have full path to the file.

The code I have used on localhost is (which when uploaded to server, it breaks the rest of the code):

if (File.Exists(path))
{
    File.Delete(path);
}

Thank you.

user1889838
  • 325
  • 4
  • 13
  • 37

1 Answers1

0

If deleting the file is possible after resettings the application pool, it seems like you're holding references to the file after uploading it. How do you handle the uploading, and what actions are performed on the file after uploading?


Update: I've found some other people with the same problem, the uploaded file stays locked by after calling SaveAs(...): File is locked after HttpPostedFile SaveAs(LocationOnServer).

Lastly, it seems the file stays locked until you call SaveAs(...), so if you have some server side validation where you decide wheter to save the file or not, maybe the problem can be there? Note that I did not test this.

Community
  • 1
  • 1
w5l
  • 5,341
  • 1
  • 25
  • 43
  • I am using `AsyncFileUpload` control and on `OnUploadedComplete` event I am saving this control into session and on a button press, I cast that session as `AsyncFileUpload`, do some checks, and then upload using 'AsyncFileUpload1.PostedFile.SaveAs(Server.MapPath(path))' to a FTP folder. That is it. I am not doing anything else after upload. Should I be doing something after`.SaveAs` ? Thanks – user1889838 Jun 10 '13 at 07:53