Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access a closed file

I have an ASP.NET FileUpload control that works fine until I deploy it. Then it works fine unless the file is over ~55kb. I think this has something to do with the postbacks and that i have to put it in a session variable. Still, it works for small files.

Is it maybe a limitation of how large a session variable can be? But it works when running from my IDE so not sure.

System.ObjectDisposedException: Cannot access a closed file

Thanks for any leads.

like image 457
user1761600 Avatar asked Mar 05 '13 21:03

user1761600


2 Answers

This can be solved by giving DiskBufferSize in the web.config:

<system.web>
    <httpRuntime executionTimeout="90" maxRequestLength="20000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192"/>
</system.web>

per Cannot access a closed file

like image 91
Øyvind Skaar Avatar answered Sep 18 '22 10:09

Øyvind Skaar


I did some testing of this, and it seems that setting DiskBufferSize is going around the problem by disabling buffering. Now the computer will use more memory. Now buffering is effectively disabled. and the performance gains you get from it are gone.

I think the correct resolution is to use the SaveAs method to save the posted file to a temporary folder on the FIRST postback, and store the file path to the temp file in viewstate or session.

After any additional post back or redirect, especially if asynchronous threads are involved, the SaveAs function is not going to work if buffering is required; you will get the error "Cannot access a closed file".

I don't know what you put in your 'session variable' but I am guessing it's the file control, which is the issue.

This resolved the issue for me, in case others are running into this but don't want to disable buffering.

like image 44
Amos Zoellner Avatar answered Sep 20 '22 10:09

Amos Zoellner