IIS Locks File and refuses to allow delete!?!?
Posted on 2010-11-08
Ok, I need some help here, I have googled this to death and found no solution that works.
Here i smy scenario:
ASP.NET form upload a image, image is possibly resized and saved to /Images/Temp folder.
At this point, the image might need to be cropped, and if so I send the user back to a page which displays the image. The user specifies what area they want to crop and hits continue.
The problem is that since IIS has now served up this image to a web browser, it is now locked by IIS. If I do utterly nothing except File.Delete(PathToFile) I will still get an access denied error saying the file is in use.
People are uploading 3MB files to resize/crop and I need the ability to delete them out of this Temp folder after the smaller cropped image is saved.
The only way I can delete the file now is to wait about 20 minutes (I guess the session is timing out?) or restart the app pool for the website. Both are not acceptable solutions.
I tried disabling output caching in IIS 7.5 for my temp folder, and it created a web.config file in the Temp folder that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<add extension=".jpg" policy="DontCache" kernelCachePolicy="DontCache" />
However, I STILL can't delete the file. I have to show it to the user (so they not what to crop) and then I want it GONE out of the /Images/Temp folder.
How can I get IIS to let go of the file so I can delete it?
Any help would be appreciated.