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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With