3

Here is my code for the Force Download:

        // URL = Download.aspx?Url=How to use the Application.txt    

        string q = Request.QueryString["Url"].ToString();

        Response.Clear();
        Response.AddHeader("Content-disposition", "Attachment; Filename=" + file);
        Response.ContentType = "Text/Plain";
        Response.WriteFile(Server.MapPath("Directory/" + q));
        Response.End();

The Dialog Box that apears in Firefox says: You are going to open the file: And the filename is displayed just ass How (the name should be: How to use the Application.txt ). The sama I mentioned if I try to wright the filename for my self:

Response.AddHeader("Content-disposition", "Attachment; Filename=How to use the Application.txt");

The same apears. Please Help!

SDwarfs
  • 3,189
  • 5
  • 31
  • 53
coceban.vlad
  • 509
  • 1
  • 7
  • 23

2 Answers2

2

Mime files names should be double quoted.

Response.AddHeader("Content-disposition", 
                   "Attachment; Filename=\"" + file + "\"");
    

This can be found in RFC 2616(HTTP 1.1)

Content-Disposition: attachment; filename="fname.ext"

Revised in RFC 6266 to allow file names without quotes too if they didn't contain disallowed charecters like spaces.

Content-Disposition: Attachment; filename=example.html

Community
  • 1
  • 1
nunespascal
  • 17,584
  • 2
  • 43
  • 46
2

You should put double quotes around the filename. Here is how to do that:

    string q = Request.QueryString["Url"].ToString();

    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment; filename=\""
        + file + "\"");
    Response.ContentType = "text/plain";
    Response.WriteFile(Server.MapPath(d + q));
    Response.End();

Note that I also changed your Strings upper/lower case to now "Content-Disposition", "attachment", "filename", "text/plain". You should use them in that way to not get into trouble with browsers that handle that quite strict.

If that doesn't work correctly, try:

    Response.AddHeader("Content-Disposition", "Attachment;
        Filename=\"" + HttpUtility.UrlEncode(file) + "\"");

Then the spaces in filenames are URL encoded.

SDwarfs
  • 3,189
  • 5
  • 31
  • 53
  • +1, had the exact same problem yesterday - where were you? ;) – Jeff Aug 17 '12 at 06:58
  • Either at work or asleep; In case you know how to enable that nifty "Omnipresence"-feature for real life... just let me know. I would give you a bounty of 300, thought ;-) – SDwarfs Aug 17 '12 at 08:28
  • According to Microsoft, it SHOULD be ready in .NET 7.5 - this.Omnipresence.Enabled = true; – Jeff Aug 17 '12 at 09:20
  • Is there an OpenSource alternative? I actually don't want a Microsoft product being omnipresent together with me ;-) – SDwarfs Aug 17 '12 at 09:25
  • Probably, if the Mono team get it together :P – Jeff Aug 17 '12 at 11:47