简体   繁体   English

用C#下载文件

[英]Download files in C#

I am trying to download a file (includes pdf, zip, images, etc) in C#. 我正在尝试使用C#下载文件(包括pdf,zip,图像等)。 I have tried the code in the following link. 我已经尝试了以下链接中的代码。

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

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

It's working as expected in IE. 它在IE中按预期工作。 But in Firefox the downloaded zip and image file is corrupted. 但是在Firefox中,下载的zip和图像文件已损坏。

The problem may be with the FILENAME parameter in the Content-Disposition header. 问题可能出在Content-Disposition标头中的FILENAME参数上。

You can try the sample code by following these rules: 您可以按照以下规则尝试示例代码:

  • the filename should be in US-ASCII charset 文件名应采用US-ASCII字符集
  • the filename should not have any directory path information specified 文件名不应指定任何目录路径信息
  • the filename should not be enclosed in double quotes 文件名不应该用双引号引起来
  • Content-Type header should refer to an unknown MIME type 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(); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM