Upload and deleting a file - error:

Discussion in 'ASP.NET 2.0' started by kj60, Sep 13, 2006.

  1. Hi

    In our site we have an area where a user can upload a file through an asp.net page and delete a file using an asp.net page. The file uploads seem to work fine but sometimes (not always) a user cannot delete a file from the server. They get the following error where the file seems to get locked:


    'Server Error in '/' Application.
    --------------------------------------------------------------------------------

    The process cannot access the file 'E:\web\teamsite\htdocs\sites\files\site25\images\p224844aa.jpg' because it is being used by another process.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.IO.IOException: The process cannot access the file 'E:\web\teamsite\htdocs\sites\files\site25\images\p224844aa.jpg' because it is being used by another process.'



    As I said this happens only intermittently and I have no idea why. At the moment we have to recycle using IIS to unlock the file.

    I have looked for a code solution but I have not found anything really to help or explain why - there seems to be many others with the same problem. I have enclosed our file upload and delete code snippets at the end of this message if anyone can help (with code samples preferably). It has been suggested that we 'close the file after use' - however I am not clear excatly what this means and how do this in (vb.net) code.

    Thanks in advance.

    Kevin



    ==========================
    UPLOAD CODE SNIPPET - saving the file


    MyFile.PostedFile.SaveAs(StrPhysicalApplicationPath & '\sites\sitefiles\site' & intSiteID & '\image\' & strFileName)




    DELETE FILE CODE SNIPPET - deleting the file


    File.Delete(StrPhysicalApplicationPath & 'sites\sitefiles\site' & intSiteID & '\image\' & strFileName)
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    Have you tried clearing these objects by

    MyFile.Dispose()
    MyFile = Nothing

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  3. Bruce

    Bruce DiscountASP.NET Staff

  4. Thanks Bruce. After many hours I think I have tracked the problem down - but not yet found a fix.

    The page shows thumbnail images in a Gridview - so that the user can choose to delete certain images. If the image is over a certain pixel size a thumbnail is generated. I have found that the images that are under this size limit can be deleted OK - the ones that are generated using the thumbnail code cannot always be deleted - so it looks like the page that generates these thumbnails is locking the image. This page does use Dispose() but this does not seem to make any difference. Will keep trying...
     
  5. so you are saying you've tracked your problem down to the generation of thumbnails? can you post the code that generates the thumbs?

    I've had this problem before, and I was able to fix it by manually forcing a Dispose() on the file object. My best guess would be that you can fix this if you figure out what object to properly Dispose() of.


    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     
  6. Bruce

    Bruce DiscountASP.NET Staff

    hmm... not cool.

    Another idea is to run a schedule task to clean out these files. You can make sure of the Scheduled Task tool.

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     

Share This Page