This is a migrated thread and some comments may be shown as answers.

Temporary files locked after upload

3 Answers 356 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Jason Rosensweig
Top achievements
Rank 1
Jason Rosensweig asked on 07 Oct 2010, 09:17 PM
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.

3 Answers, 1 is accepted

Sort by
0
Jason Rosensweig
Top achievements
Rank 1
answered on 07 Oct 2010, 09:35 PM
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.
0
Genady Sergeev
Telerik team
answered on 12 Oct 2010, 04:32 PM
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
0
Jason Rosensweig
Top achievements
Rank 1
answered on 12 Oct 2010, 04:50 PM
Ha! Duh...Thanks for (what should have been) an obvious answer! A+ as always.

J
Tags
AsyncUpload
Asked by
Jason Rosensweig
Top achievements
Rank 1
Answers by
Jason Rosensweig
Top achievements
Rank 1
Genady Sergeev
Telerik team
Share this question
or