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!

1

2 Answers 2

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

0
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.

5
  • +1, had the exact same problem yesterday - where were you? ;)
    – Jeff
    Aug 17, 2012 at 6: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, 2012 at 8:28
  • According to Microsoft, it SHOULD be ready in .NET 7.5 - this.Omnipresence.Enabled = true;
    – Jeff
    Aug 17, 2012 at 9:20
  • Is there an OpenSource alternative? I actually don't want a Microsoft product being omnipresent together with me ;-)
    – SDwarfs
    Aug 17, 2012 at 9:25
  • Probably, if the Mono team get it together :P
    – Jeff
    Aug 17, 2012 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.