【问题标题】:C# ASP.NET 3.5 content-disposition file download problemsC# ASP.NET 3.5 内容配置文件下载问题
【发布时间】:2010-09-25 01:56:07
【问题描述】:

我有一个存储一些文档的 sql 数据库。

用户可以登录应用程序,并查看他们的文档列表。

在他们的文档的网格视图中单击链接按钮下载时,我从数据库中获取文件,将其写入文件系统,然后执行此代码。

    System.IO.FileInfo file = new System.IO.FileInfo(System.Configuration.ConfigurationManager.AppSettings["UploadPath"] + DocumentName);

    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Cookies.Clear();
    Response.Cache.SetCacheability(HttpCacheability.Private);
    Response.CacheControl = "private";
    Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
    Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
    Response.AppendHeader("Content-Length", file.Length.ToString());
    Response.AppendHeader("Pragma","cache");
    Response.AppendHeader("Expires", "60");
    Response.ContentType = GetContentType(file.Extension);
    Response.AppendHeader("Content-Disposition",
    "inline; " +
    "filename=\"" + file.Name + "\"; " +
    "size=" + file.Length.ToString() + "; " +
    "creation-date=" + DateTime.Now.ToString("R") + "; " +
    "modification-date=" + DateTime.Now.ToString("R") + "; " +
    "read-date=" + DateTime.Now.ToString("R"));

我的 GetContentType() 方法只为我允许的文件“application/pdf、application/msw0rd 等”返回适当的文件类型。

我的问题是,当文件被保存时,它是网页本身,而不是文件系统中的文件。而在谷歌浏览器中,它在文件名的末尾添加了一个 .htm 扩展名,我猜是因为它知道这是一个网页?

无论如何,第一步是获取实际文件,而不是他们所在的 HTML 网页副本!

谢谢。

【问题讨论】:

    标签: c# asp.net content-disposition


    【解决方案1】:

    您是否尝试过将内容处置设置为“附件”而不是“内联”?我相信浏览器会提示用户打开或保存文档。

    此外,您通常可以通过使用 BinaryWrite 方法将数据库中的字节流直接写入 Response 对象来绕过文件系统...这也是您可能希望使用 HTTP 处理程序而不是在ASPX 页面,这样您就不必担心清除响应等。我过去曾在页面上使用隐藏的 iframe,然后使用 Javascript 将其 src 设置为采用文档 ID 的 HTTP 处理程序。作为 QueryString 上的参数。

    【讨论】:

      【解决方案2】:

      代替

      Response.AppendHeader("内容配置", "内联;" + "文件名=

      使用

      Response.AddHeader("content-disposition:", "attachment;filename=

      【讨论】:

        【解决方案3】:

        您如何发送文件的实际内容?

        我通常使用Response.TransmitFile方法,它基本上是打开文件并将其内容发送到Response.OutputStream

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-09-24
          • 1970-01-01
          • 1970-01-01
          • 2014-06-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多