I’m trying to use Controller.File to return a FilePathResult from a view in my ASP.NET MVC 2 project. I use it like this:
return File(pdfFilePath, "application/pdf", "foo.pdf");
However I keep getting this error in the yellow screen of death:
The process cannot access the file [the file path] because it is being used by another process.
This error usually comes up when you forget to close a file stream, but I figured that this should be taken care of by the ASP.NET MVC framework. This doesn’t happen every time, but rather periodically. Sometimes I get the file just fine but then it just stops working. I am using the development server when testing this.
Any ideas?
Are you accessing the file prior to the line of code you provided? If so, how are you accessing it?
When accessing files, try to use the following to avoid file stream conflicts:
The last enum
FileShare.ReadWritewill allow other file streams to read and write to the file even if you have it open. Of course, it is better to remember to close your stream ASAP.http://msdn.microsoft.com/en-us/library/y973b725.aspx