Application.download() Question

0
0

I’m using the PDFSharp library to do some manipulations on pdf files and return them to the browser.

Question:

I originally had this code:

private MemoryStream ExportAllToSinglePDF()

….. more code that manipulates a pdf file in memory ……

// Send the new PDF to the browser

using (MemoryStream lo_stream = new MemoryStream())
{
outputDocument.Save(lo_stream,false);
return lo_stream;
}

……..

// Called by this code…

Application.Download(ExportAllToSinglePDF(), ls_WbName);

 

But i was getting an error that the stream was already closed when it hit Application.Download().

 

So I changed private MemoryStream ExportAllToSinglePDF() to this:

MemoryStream lo_stream = new MemoryStream();
outputDocument.Save(lo_stream);
lo_stream.Position = 0;
return lo_stream;

The question is this: does Application.Download close the stream for me after my new code ? Or am i going to leak memory like a spaghetti strainer ?

 

 

  • You must to post comments
0
0

In the first sample the stream was closed by the using block.

You can pass the stream to Application.Download() and then dispose it. Wisej doesn’t dispose the stream, it doesn’t know if the app uses it again. Internally it saves the content into a temp file immediately and when the request to download it comes in, it will simply return the file and delete it from the temp directory.

 

 

  • You must to post comments
0
0

I changed the code to this last night, which I think would ensure the memory stream gets closed and cleaned up:

// Return the document to client as a stream
using (MemoryStream lo_stream = new MemoryStream())
{
outputDocument.Save(lo_stream);
lo_stream.Position = 0;
Application.Download(lo_stream, ls_WbName);
}

Still not sure if the Application.Download closes the stream on its own.

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.