How do I get the browser to prompt "Open / Save" when this stream is returned?
I'm using Crystal Reports and have some开发者_开发问答 code that returns a stream of a PDF report. When The report is done being generated, the browser (IE8) prompts for the user to open or save the report. It works but behind the scenes an exception is being thrown. We handle the exception but the handling causes some other problems.
// This completes but throws an exception.
rpt.ExportToHttpResponse(crExportOptions.ExportFormatType, resp, true, fileName);
The solution according to a post on the SAP site works and the exception is no longer thrown. However, the problem is we no longer have the option to open or save the file. The report is just opened in the browser.
System.IO.Stream oStream = null;
byte[] byteArray = null;
oStream = report.ExportToStream(crExportOptions.ExportFormatType);
byteArray = new byte[oStream.Length];
oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));
response.ClearContent();
response.ClearHeaders();
response.ContentType = "application/pdf";
response.BinaryWrite(byteArray);
response.Flush();
response.Close();
report.Close();
report.Dispose();
I want my cake and I want to eat it! I don't want the exception thrown (solved by the SAP solution) but still want the option to open or save the PDF returned. How do I modify the above code the present the user with the option to save / open?
Thanks!
Found a solution in this SO post.
added this to my code...
response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", reportName));
精彩评论