简体   繁体   中英

Download files in C#

I am trying to download a file (includes pdf, zip, images, etc) in C#. I have tried the code in the following link.

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

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

It's working as expected in IE. But in Firefox the downloaded zip and image file is corrupted.

The problem may be with the FILENAME parameter in the Content-Disposition header.

You can try the sample code by following these rules:

  • the filename should be in US-ASCII charset
  • 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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