【问题标题】:Download files in C#在 C# 中下载文件
【发布时间】:2012-02-25 12:49:29
【问题描述】:

我正在尝试用 C# 下载文件(包括 pdf、zip、图像等)。 我已经尝试了以下链接中的代码。

http://www.daniweb.com/web-development/aspnet/threads/252778

http://dotnetslackers.com/Community/blogs/haissam/archive/2007/04/03/Downloading-Files-C_2300_.aspx

它在 IE 中按预期工作。但在 Firefox 中,下载的 zip 和图像文件已损坏。

【问题讨论】:

  • 损坏了怎么办?预期和实际有什么区别?另外:在这种情况下,您没有尝试下载 - 您正在向客户端提供 http 响应,并提示客户端将其视为下载 - 非常不同。
  • 您是否在服务器上进行任何压缩,可能在Gloabl.asax?如果是,请跳过它以获取下载请求。

标签: c# asp.net download


【解决方案1】:

问题可能出在 Content-Disposition 标头中的 FILENAME 参数上。

您可以按照以下规则尝试示例代码:

  • 文件名应为 US-ASCII 字符集
  • 文件名不应指定任何目录路径信息
  • 文件名不应用双引号括起来
  • Content-Type 标头应该引用未知的 MIME 类型

    FileInfo fi = new FileInfo(@"c:\picture.bmp");
    Response.Clear();
    Response.ContentType = "application/x-unknown";
    Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", System.Web.HttpUtility.UrlPathEncode(fi.Name)));
    Response.AddHeader("Content-Length", fi.Length.ToString());
    Response.TransmitFile(fi.FullName);
    Response.End();
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 2013-08-27
    • 2021-09-21
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    相关资源
    最近更新 更多