Temporary files locked after upload

4 posts, 0 answers
  1. Jason Rosensweig
    Jason Rosensweig avatar
    9 posts
    Member since:
    Feb 2010

    Posted 07 Oct 2010 Link to this post

    I am using the following method on RadAsyncUpload_FileUploaded:

    protected void upAvatar_FileUploaded(object sender, FileUploadedEventArgs e)
    {
    byte[] imageData = new byte[e.File.InputStream.Length];

    using (Stream stream = e.File.InputStream)
    {
        stream.Read(imageData, 0, (int)e.File.InputStream.Length);
    }

    Thumbnail.DataValue = imageData;
    }

    This is from the Ajax Processing demo under Asynchronous Upload. The function works fine however the temp file is locked and cannot be deleted for approximately 5 minutes. If I comment out the contents of this function, the file can be deleted immediately. I am setting TemporaryFileExpiration = new TimeSpan(0, 0, 10) so when the file delete occurs an event is logged in the Event Viewer similar to this: 

    Exception information: 
        Exception type: IOException 
        Exception message: The process cannot access the file 'h:\Websites\Project\App_Data\RadUploadTemp\1ng3c0wt.bdu' because it is being used by another process.
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.File.Delete(String path)
       at Telerik.Web.UI.AsyncUploadHandler.RemovedCallback(String key, Object value, CacheItemRemovedReason reason)
       at System.Web.Caching.CacheEntry.CallCacheItemRemovedCallback(CacheItemRemovedCallback callback, CacheItemRemovedReason reason)

    I attempted to do a stream copy to work with the contents of the uploaded file separately but the temporary file still remains locked. It seems that a simple read of e.File.InputStream locks the file. Is there any way around this? The only way I have found so far is to set the TemporaryFileExpiration to a greater value (>10min). Thanks.

  2. Jason Rosensweig
    Jason Rosensweig avatar
    9 posts
    Member since:
    Feb 2010

    Posted 07 Oct 2010 Link to this post

    Actually setting the TemporaryFileExpiration to 10 min or more doesn't make a difference. I'm not sure why it worked once but it no longer does. Manually deleting the files throws a message: The action can't be completed because the file is open in w3wp.exe

    I am running Windows 7 and v2010.2.929.40 of the toolset.
  3. Genady Sergeev
    Admin
    Genady Sergeev avatar
    1600 posts

    Posted 12 Oct 2010 Link to this post

    Hello Jason Rosensweig,

    The problem comes from this line:

    byte[] imageData = new byte[e.File.InputStream.Length];

    Please, use

    byte[] imageData = new byte[e.File.ContentLength];

    instead. The problem is that InputStream.length will create a new reference pointing to the file and since it is not inside a using statement it will keep the file locked.

    Best wishes,
    Genady Sergeev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  4. Jason Rosensweig
    Jason Rosensweig avatar
    9 posts
    Member since:
    Feb 2010

    Posted 12 Oct 2010 Link to this post

    Ha! Duh...Thanks for (what should have been) an obvious answer! A+ as always.

    J
Back to Top