Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who's responsible for disposing HttpPostedFile.InputStream - and when?

Tags:

asp.net

When you receive an uploaded file in ASP.NET, you generally do so via an HttpPostedFile object. The received data is made available via HttpPostedFile.InputStream. This is a property, which would lead me to believe I don't need to dispose it myself, however the documentation never mentions who is responsible for disposing the stream, and if it's done by the ASP.NET framework, when it does so (say, can I save the stream in the session, should I wish to?).

Now, I don't receive that many files, and I've not run into problems for not disposing this particular stream, but for cleanliness - does anyone know what the design contract here is?

like image 573
Eamon Nerbonne Avatar asked Jul 24 '09 09:07

Eamon Nerbonne


1 Answers

From my experience working with it I would say that it is released as soon as the request processing is complete. An example is if I were to throw a ball into the air. If I don't catch it and it hits the ground...it is disposed of. If I catch it and do something with it...and then drop it is disposed of. It doesn't hang around for you to play with in the next request!

Addressing the issue of storing it in the session I would say NOOOOOO! If you need it to be around for a while write it to the file system. When you need it again reconstitute it as a stream and play with it. Storing a file stream in the session (as a byte array perhaps?) sounds like a huge waste of your session (memory) resources.

like image 77
Andrew Siemer Avatar answered Sep 28 '22 00:09

Andrew Siemer