Search code examples
c#asp.nethttphttp-headersforce-download

When I force downloading a file from a page, it split te file name by SPACE /C#?


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!


Solution

  • 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