首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >防止代码提示用户保存文件

防止代码提示用户保存文件
EN

Stack Overflow用户
提问于 2014-12-05 21:02:05
回答 1查看 123关注 0票数 0

我在我的报表控制器中使用了下面的方法。它成功地返回了我在数据库中分配给varBinary(MAX)字段的文件内容。我的问题是,当这段代码执行时,它会导致浏览器提示用户保存或打开文件。我想阻止这一切的发生。

如何强制它只将二进制数据返回到调用控制器方法,而不将结果推送到客户端浏览器?

代码语言:javascript
复制
private FileContentResult RenderReportFile(LocalReport localReport, List<ReportDataSource> listDS, string sFilename, string sReportType, bool bLandscape)
{
    string sHeight = "11";
    string sWidth = "8.5";

    if (bLandscape)
    { sWidth = sHeight; sHeight = "8.5"; }

    foreach (ReportDataSource ds in listDS)
    {
        localReport.DataSources.Add(ds);
    }

    HttpContextBase imageDirectoryPath = HttpContext;

    string reportType = sReportType;
    string mimeType;
    string encoding;
    string fileNameExtension;

    //The DeviceInfo settings should be changed based on the reportType
    string deviceInfo =
        "<DeviceInfo>" +
        "  <OutputFormat>" + sReportType + "</OutputFormat>" +
        "  <PageWidth>" + sWidth + "in</PageWidth>" +
        "  <PageHeight>" + sHeight + "in</PageHeight>" +
        "  <MarginTop>0.5in</MarginTop>" +
        "  <MarginLeft>0.5in</MarginLeft>" +
        "  <MarginRight>0.5in</MarginRight>" +
        "  <MarginBottom>0.5in</MarginBottom>" +
        "</DeviceInfo>";

    Warning[] warnings;
    string[] streams;
    byte[] renderedBytes;

    //Render
    renderedBytes = localReport.Render(
        reportType,
        deviceInfo,
        out mimeType,
        out encoding,
        out fileNameExtension,
        out streams,
        out warnings);


    //Write to the outputstream
    //Set content-disposition to "attachment" so that user is prompted to take an action
    //on the file (open or save)

    Response.Clear();
    Response.ContentType = mimeType;
    Response.AddHeader("content-disposition", "attachment; filename=" + sFilename + "." + fileNameExtension);
    Response.BinaryWrite(renderedBytes);
    Response.End();
    return File(renderedBytes, "application/pdf", sFilename + "." + fileNameExtension);
}
EN

回答 1

Stack Overflow用户

发布于 2014-12-05 21:05:53

此代码负责将文档发送到客户端:

代码语言:javascript
复制
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=" + sFilename + "." + fileNameExtension);
Response.BinaryWrite(renderedBytes);
Response.End();

删除它,用户将不会被提示保存或打开。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27316516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档