Me Myself & C#

Manoj Garg’s Tech Bytes – What I learned Today

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 lightbulb

Advertisements

8 Responses to “Writing a MemoryStream to Response Object for opening a Open / Save / Cancel Dialogue in ASP.NET 2.0”

  1. Edward Meshuris said

    Hi,
    Thank you very much for this code. Saved me some time.

    -Edward

  2. Chris said

    Thanks for this post it saved me a lot of trial and error.

    Chris

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

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

  5. 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!

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

  7. 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#[…]…

  8. yugdfjsnd said

    I had to discover ms is a MemoryStream but thanks a lot.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: