如何让浏览器在返回此流时提示"Open / Save"?



我正在使用Crystal Reports,并且有一些返回PDF报告流的代码。当报表生成完成后,浏览器(IE8)提示用户打开或保存报表。它可以工作,但是在幕后抛出了一个异常。我们处理了这个异常,但是这个处理引起了一些其他的问题。

// This completes but throws an exception.
rpt.ExportToHttpResponse(crExportOptions.ExportFormatType, resp, true, fileName); 

根据SAP站点上的一篇文章的解决方案可以工作,并且不再抛出异常。然而,问题是我们不再有打开或保存文件的选项。报告刚刚在浏览器中打开。

            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();

我要我的蛋糕,我要吃它!我不希望抛出异常(由SAP解决方案解决),但仍然希望打开或保存返回的PDF。我如何修改上面的代码,为用户提供保存/打开的选项?

谢谢!

在这篇文章中找到了一个解决方案。

将此添加到我的代码中…

response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", reportName)); 

最新更新