Bavya
Bavya

Reputation: 143

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.

Upvotes: 1

Views: 1257

Answers (1)

Alberto Spelta
Alberto Spelta

Reputation: 3678

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

Upvotes: 1

Related Questions