Solved

IIS Locks File and refuses to allow delete!?!?

Posted on 2010-11-08
9
2,426 Views
Last Modified: 2012-06-27
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"?>
<configuration>
    <system.webServer>
        <caching>
            <profiles>
                <add extension=".jpg" policy="DontCache" kernelCachePolicy="DontCache" />
            </profiles>
        </caching>
    </system.webServer>
</configuration>


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.
0
Comment
Question by:adampirata
9 Comments
 
LVL 5

Expert Comment

by:jijeesh
ID: 34090085
Can you check whther the files are closed after processing it.
0
 
LVL 12

Expert Comment

by:Nirmalan Nagenthiran
ID: 34090464
Try some thing once you write the file to the browser

 //Write the File to browser
 Response.WriteFile(file.FullName);
 Response.Flush();
 Response.End();
0
 

Author Comment

by:adampirata
ID: 34092320
NNirmalan, there is no place for me to put that code, I am not "wrting" the file to Response, I am simply calling it in a IMG tag.

Let me throw in some more details to see if that helps.

Steps to reproduce:
1) File is uploaded via asp:FileUpload.
2) File is saved. If I wanted to delete it here with File.Delete(FileName) it works just fine. This isn't where I want it delete though.
3) Next this:
ImageToCrop.ImageUrl =  "/Images/Temp/" + sFileName;
mvMain.SetActiveView(vEditImage);

All this does is present the user the image in a browser. At this point I am now screwed for deleting the file.
4) I put a button under the image and on the click event, I tried deleting the file. At this point it is now locked by IIS because IIS served up the file.

I already tried making the ImageURL dynamic like GetImage.aspx?Path=/Images/Temp, figuring I could control the opening and closing of the file....the file cropping DLL I am using WebCropImage.UI won't work with a dynamic URL.

I have definately pinpointed that setting this: ImageToCrop.ImageUrl =  "/Images/Temp/" + sFileName; and then navigating to that page is what is locking the file.

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34095215
Hmm, you must have something else going on...the code below works as expected for me on both IIS 7 and IIS 6.
Are you disposing of any objects from WebCrop.UI that might have opened the file, and does your web application have the necessary NTFS permissions to delete files?

<%@ Page Language="C#" %>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<script runat="server">

	private void UploadButton_Click(object sender, EventArgs e)

	{

		if (ImageUploader.HasFile)

		{

			string imageUrl = "~/Temp/" + System.IO.Path.GetRandomFileName() + ".jpg";

			ImageUploader.SaveAs(Server.MapPath(imageUrl));

			ImageToCrop.ImageUrl = imageUrl;

		}

	}



	private void DeleteImageButton_Click(object sender, EventArgs e)

	{

		System.IO.File.Delete(Server.MapPath(ImageToCrop.ImageUrl));

		ImageToCrop.ImageUrl = String.Empty;

	}

</script>



<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:FileUpload id="ImageUploader" runat="server" /><br />

	<asp:Button ID="UploadButton" runat="server" Text="Upload Image" OnClick="UploadButton_Click" />

	<br />

	<asp:Button ID="DeleteImageButton" runat="server" Text="Delete Image" OnClick="DeleteImageButton_Click" />

	<br />

	<asp:Image id="ImageToCrop" runat="server" />

    </div>

    </form>

</body>

</html>

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34095252
...with what are basically default settings in both cases, and a mostly-empty web.config:

<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0">
</compilation>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
</modules>
</system.webServer>
</configuration>
0
 

Author Comment

by:adampirata
ID: 34095329
@tgerbert

I will try your code and see if that works on my server.  I did actually give full control to EVERYONE for that temp folder and subdirectories, so it shouldn't be a permission issue. I know that isn't a solution in the long term, but it should rule out permisssions "for now".

Let me try your code and see if I can identify some differences to narrow down my issue.

Thank you.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34096704
I've been monkying around a little bit with the WebCrop control - it seems this has locked the file, so you will need to either 1) find an alternate control that does the same thing (without leaving the file locked), 2) modify the source code for this control so you can make sure it's releasing resources (I already tried calling .Dispose() on the WebCrop object with no success), or 3) Live with a bunch of temp files on the server and clear'em out periodically.
(Actually, calling WebCrop.Dispose() followed by GC.Collect() works - but you don't want to be calling GC.Collect in a production web app, though it does indicate the WebCrop control might have an empty Dispose() implementation)
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 34096849
Actually, it looks like the culprit might be just one function in the WebCrop tool - you can download the source code for the WebCrop object, modify it and re-compile it.
The code as is reads (since the FileStream fs isn't disposed of explicitly the image file remains locked until garbage collection runs):
private bool GetRawSize(string rawimage)
{
    FileStream fs = new FileStream(rawimage, FileMode.Open, FileAccess.Read, FileShare.Read);
    System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
    try
    {
        rawWidth = image.Width;
        rawHeight = image.Height;
    }
    catch (Exception ex)
    { throw ex; }
    return true;
}
Put it inside a "using" block might do the trick:

private bool GetRawSize(string rawimage)

        {

            using (FileStream fs = new FileStream(rawimage, FileMode.Open, FileAccess.Read, FileShare.Read))

            {

                System.Drawing.Image image = System.Drawing.Image.FromStream(fs);



                try

                {

                    rawWidth = image.Width;

                    rawHeight = image.Height;

                }

                catch (Exception ex)

                { throw ex; }

                return true;

            }

        }

Open in new window

0
 

Author Closing Comment

by:adampirata
ID: 34128874
Thank you so much for your help. It never occured to me that the WebCrop tool would lock the file when I wasn't actually doing a crop with it. Apparently it had to get the file dimensions just to render on the screen...and as you found out, they didn't release the file.

All is working now...you saved me hours of frustration, thank you!
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now