简体   繁体   English

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

[英]Prevent code from prompting user to save file

I use this method below in my report controller. 我在报表控制器中使用以下方法。 It succesfully returns file content which I assign to a varBinary(MAX) field in my database. 它成功返回文件内容,该文件内容我分配给数据库中的varBinary(MAX)字段。 My problem is, when this code executes it causes the browser to prompt the user to SAVE or OPEN the file. 我的问题是,执行此代码时,它会导致浏览器提示用户保存或打开文件。 I want to stop this from happening. 我想阻止这种情况的发生。

How can I force it to only return the binary data to the calling controller method and not push results to the client browser? 如何强制它仅将二进制数据返回给调用控制器方法,而不将结果推送到客户端浏览器?

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

This code is responsible for sending the document to the client: 此代码负责将文档发送给客户端:

Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=" + sFilename + "." + fileNameExtension);
Response.BinaryWrite(renderedBytes);
Response.End();

Remove it and the user will not be prompted for save or open. 删除它,不会提示用户保存或打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不提示用户输入名称/路径的情况下保存文件? - How to save a file without prompting the user for a name/path? 在关闭程序时提示用户保存之间减少代码重复的优雅方法? - Elegant way to reduce code duplication between prompting the user to save when closing the program? 将文件写入HttpResponse,浏览器不提示保存 - Writing file to HttpResponse, browser not prompting to save 如何防止SaveFileDialog提示两次替换/覆盖文件? - How do I prevent a SaveFileDialog from prompting twice to replace/overwrite a file? 如何保存C#代码中的用户响应并阻止对单选按钮的多次选择 - How do I Save the user response from the C# Code and prevent multiple selection of Radio Button 自动保存文件,而不提示您将其保存在Web浏览器中 - Save file automatically without prompting to save it in web browser 提示用户将当前行保存在DavaGridView_RowLeave上 - Prompting message to user to Save the current row on DavaGridView_RowLeave 如何防止用户手动更改文件? - How to prevent a user from changing a file manually? 防止用户删除,移动或重命名文件 - Prevent a user from deleting, moving or renaming a file 提示用户选择要写入文件的文件夹的标准方法? - Standard way of prompting user to select folder to write a file to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM