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?
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.
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