Writing a MemoryStream to Response Object for opening a Open / Save / Cancel Dialogue in ASP.NET 2.0
Posted by Manoj Garg on July 17, 2008
Following code block can be used to show user on Open/Save/Cancel dialogue box in browser for a memory stream.
if (ms != null)
{
Byte[] byteArray = ms.ToArray();
ms.Flush();
ms.Close();
Response.BufferOutput = true;
// Clear all content output from the buffer stream
Response.Clear();
//to fix the “file not found” error when opening excel file
//See http://www.aspose.com/Community/forums/ShowThread.aspx?PostID=61444
Response.ClearHeaders();
// Add a HTTP header to the output stream that specifies the default filename
// for the browser’s download dialog
string timeStamp = Convert.ToString(DateTime.Now.ToString(“MMddyyyy_HHmmss”));
Response.AddHeader(“Content-Disposition”,
“attachment; filename=testFileName_” + timeStamp + “.fileextention”);
// Set the HTTP MIME type of the output stream
Response.ContentType = “application/octet-stream”;
// Write the data
Response.BinaryWrite(byteArray);
Response.End();
}
Ha-P Coding
Edward Meshuris said
Hi,
Thank you very much for this code. Saved me some time.
-Edward
Chris said
Thanks for this post it saved me a lot of trial and error.
Chris
wael said
thank you
but when put this code in page load , the page Is dispaly for
2 second AND not appear
can you help me
wael said
thank you
but when put this code in page load , the page Is dispaly for
2 second AND not appear
and open,save,cancel not appearance
can you help me
Abel said
Thank you. Concise and to the point. Well documented as well. Scanned through it for a few seconds and was able to figure out the lines I need for my requirement.
Thanks!
Schecker said
This is a strongest place for such kind of articles, your website is a inspiration for me. i got so very much benefits and great results after visiting here and the grace is increasing day by day in your posts. The above information is extremly essential.
Anne-lise said
Anne-lise…
[…]Writing a MemoryStream to Response Object for opening a Open / Save / Cancel Dialogue in ASP.NET 2.0 « Me Myself & C#[…]…
yugdfjsnd said
I had to discover ms is a MemoryStream but thanks a lot.