Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I dispose my filestream when implementing a file download in ASP.NET?

Tags:

I have a class DocumentGenerator which wraps a MemoryStream. So I have implemented IDisposable on the class.

I can't see how/where I can possibly dispose it though.

This is my current code, which performs a file download in MVC:

using (DocumentGenerator dg = DocumentGenerator.OpenTemplate(path)) {     /* some document manipulation with the         DocumentGenerator goes here ...*/      return File(dg.GetDocumentStream(), "text/plain", filename); } 

This errors as the stream is closed/disposed before the controller has finished with it. How can I make sure my resources are properly disposed in this situation?

EDIT: My implementation of IDisposable at the moment just disposes the MemoryStream. I know it's not a proper implementation, I just used it as a test. Is there something different I could do here to make it work?

public void Dispose() {     _ms.Dispose();     _ms = null; } 
like image 436
fearofawhackplanet Avatar asked Jun 21 '10 11:06

fearofawhackplanet


People also ask

How do I dispose of FileStream?

In the specific case of a FileStream , you don't need to dispose it to close the file, you only need to use the Close method.

What is dispose in asp net?

The ASP.NET MVC framework calls Dispose when the request has completed processing. Developers typically do not have to call Dispose.

What is the use of FileStream in C#?

The FileStream is a class used for reading and writing files in C#. It is part of the System.IO namespace. To manipulate files using FileStream, you need to create an object of FileStream class. This object has four parameters; the Name of the File, FileMode, FileAccess, and FileShare.


1 Answers

You don't need to dispose the stream. It will be disposed by the FileStreamResult.WriteFile method. Code excerpt from this class:

public FileStreamResult(Stream fileStream, string contentType) : base(contentType) {     if (fileStream == null)     {         throw new ArgumentNullException("fileStream");     }     this.FileStream = fileStream; }  protected override void WriteFile(HttpResponseBase response) {     Stream outputStream = response.OutputStream;     using (this.FileStream)     {         byte[] buffer = new byte[0x1000];         while (true)         {             int count = this.FileStream.Read(buffer, 0, 0x1000);             if (count == 0)             {                 return;             }             outputStream.Write(buffer, 0, count);         }     } } 

Notice the using. When you call File(dg.GetDocumentStream(), "text/plain", filename) from your controller this invokes the constructor which stores the stream into a public property which is disposed during the rendering.

Conclusion: you don't need to worry about disposing the stream obtain with dg.GetDocumentStream().

like image 171
Darin Dimitrov Avatar answered Nov 04 '22 20:11

Darin Dimitrov