【问题标题】:How to force Chrome to open an "open file dialog" when downloading a file vía ASP.NET codebehind?通过 ASP.NET 代码隐藏下载文件时,如何强制 Chrome 打开“打开文件对话框”?
【发布时间】:2012-12-05 16:36:56
【问题描述】:

我有一个向网络浏览器发送二进制文件、pdf、word 或 excel 的页面。在 Firefox 和 IE 中都会打开一个对话框,询问您要如何处理此文件,“打开”或“保存”

但是Chrome直接保存到你的电脑上。

是否可以让 Chrome 询问您要如何处理此文件,在将文件发送到浏览器之前将一些元数据放入 Web 响应中?

【问题讨论】:

    标签: c# asp.net google-chrome httpresponse


    【解决方案1】:

    无法强制 Chrome 提示保存对话框。用户必须在 chrome 的配置上更改该行为(高级设置)。

    【讨论】:

    • 那个设置在哪里?
    • 但是要求用户勾选这个复选框是不切实际的,那么有没有其他的解决方案呢?
    【解决方案2】:

    也许会有所帮助

    System.String filePath = "c:\\tempFile.pdf"
    System.IO.FileInfo fileInfo = new FileInfo(filePath);
    
    System.Web.HttpContext context = System.Web.HttpContext.Current;
    System.Web.HttpResponse response = context.Response;
    response.Clear();
    response.ClearHeaders();
    response.ClearContent();
    response.ContentType = "application/pdf";
    response.AppendHeader("content-type", "application/pdf");
    response.AppendHeader("content-length", fileInfo.Length.ToString());
    response.AppendHeader("content-disposition", String.Format("attachment; filename={0}.pdf", outputFileName));
    response.TransmitFile(filePath);
    response.Flush(); // this make stream and without it open chrome save dialog
    context.ApplicationInstance.CompleteRequest(); // send headers and c# server-side code still continue
    
    System.IO.File.Delete(filePath); // clean cache here if you need
    
    response.End();
    

    【讨论】:

      【解决方案3】:

      您需要发送一些标头,以便浏览器知道如何处理您正在流式传输的内容:

      Response.ContentType 
      Content-Disposition, application,and 
      filename=" + FileName
      

      这将强制下载。另请参阅此链接以获取更多信息:

      http://www.devtoolshed.com/aspnet-download-file-web-browser

      谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-04
        • 1970-01-01
        • 2011-07-24
        • 2015-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多