Me Myself & C#

Manoj Garg’s Tech Bytes – What I learned Today

Posts Tagged ‘response object’

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

Posted in ASP.Net, C# | Tagged: , , | 8 Comments »