HttpPostedFileBase and locking...
-
Hi all, Ok this is the problem. I am using the microsoft MVC web framework to build a web app. I have a method that uploads files to the server by retriving the HttpPostedFileBase files from the HttpFileCollectionBase in the Request. I then invoke the HttpPostedFileBase method SaveAs(path) to save those files to the filesystem. I have just implemented a Remove method that takes the filename and path, ensures the existence of the file by calling File.Exists() then attempting to call File.Delete() to remove the said file. The problem is when I have just uploaded this file, when I go then to delete it right away I get a "The process cannot access the file 'C:\....\file.jpg' because it is being used by another process." Now I suspect that that HttpPostedFileBase method SaveAs() has locked that file and not released it. But I am not 100% sure. My only other suspision is that the actual page view method goes and does a: DirectoryInfo info = new DirectoryInfo(imagesPath.ToString()); FileInfo[] files = info.GetFiles(); In order to retrieve all the files located in that directory. One of the two of these is locking that file. I am more inclined to believe its the SaveAs() method as the remove works intermittently and if it were the page view I'd expect it not to work ever. Does anyone know anything about this, I have been googling away like mad trying to find out how to get HttpPostedFileBase to release the file its just saved with no avail. Cheers.
-
Hi all, Ok this is the problem. I am using the microsoft MVC web framework to build a web app. I have a method that uploads files to the server by retriving the HttpPostedFileBase files from the HttpFileCollectionBase in the Request. I then invoke the HttpPostedFileBase method SaveAs(path) to save those files to the filesystem. I have just implemented a Remove method that takes the filename and path, ensures the existence of the file by calling File.Exists() then attempting to call File.Delete() to remove the said file. The problem is when I have just uploaded this file, when I go then to delete it right away I get a "The process cannot access the file 'C:\....\file.jpg' because it is being used by another process." Now I suspect that that HttpPostedFileBase method SaveAs() has locked that file and not released it. But I am not 100% sure. My only other suspision is that the actual page view method goes and does a: DirectoryInfo info = new DirectoryInfo(imagesPath.ToString()); FileInfo[] files = info.GetFiles(); In order to retrieve all the files located in that directory. One of the two of these is locking that file. I am more inclined to believe its the SaveAs() method as the remove works intermittently and if it were the page view I'd expect it not to work ever. Does anyone know anything about this, I have been googling away like mad trying to find out how to get HttpPostedFileBase to release the file its just saved with no avail. Cheers.
Hi daviiie; I have exactly the same issue and tried this, thinking SaveAs locking the files as you did but id didn't work, so something else is locking the files:
// Request.Files[i].SaveAs(fileName_save);
using (FileStream fs = new FileStream(fileName_save, FileMode.Create))
{
byte[] bytes = new byte[Request.Files[i].InputStream.Length];
Request.Files[i].InputStream.Read(bytes, 0, (int)Request.Files[i].InputStream.Length);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
}Did you have any progress for the issue? Thanks.