1
string filename = "Category#FileName";

response.AddHeader("content-disposition", "attachment; filename=" + filename);
response.ContentType = "text/csv";
response.AddHeader("Pragma", "public");

Final file name => Category_FileName

I would appreciate your help here, pls help!

Adnan
  • 25,882
  • 18
  • 81
  • 110
R.L. Narayanan
  • 53
  • 1
  • 11

2 Answers2

1

A filename can't contain the character / under Windows (or most other OSes), so you won't be able to stop that one getting converted. # is left alone by most browsers, the odd one out being IE.

Content-Disposition filenames are a bit limited. Non-ASCII characters aren't reliable in them and the proper escaping according to the RFC doesn't work. If you want to get Unicode characters into a default download filename​—or certain other punctuation characters including # in IE—you have to URL-encode it and include that as a trailing part of the path, omitting the Content-Disposition filename. eg:

http://www.example.com/myscript.aspx/foo%23bar.xls

myscript.aspx:

response.AddHeader("Content-Disposition", "attachment");

(You still can't include / in a filename this way as web servers tend to block all URLs with %2F in. In any case, as above, you wouldn't be able to save it with a / in the filename anyway.)

bobince
  • 528,062
  • 107
  • 651
  • 834
  • Thanks Bobince!! UrlEncode works great with "#". Thanks a ton! – R.L. Narayanan Dec 06 '10 at 17:52
  • Ah, .NET's `UrlEncode` may not be the right thing for a space character, though: it'll encode it to `+` instead of `%20`, which isn't quite right for path components. [Background here](http://stackoverflow.com/q/2678551/18936). It's a bit unfortunate there is no non-form-URL-encode method in .NET, but you can fix it manually by replacing `+` with `%20` after encoding. – bobince Dec 06 '10 at 20:12
  • Yes Bobince, i too experienced the same issue of "+" for space. Then went with manual replace. Thought of responding to your previous suggestion but got the comments from you as well :) Thanks again – R.L. Narayanan Dec 08 '10 at 08:23
0

Sadly this is a limitation of IE.

According to Microsoft, the following characters will be converted to underscores if you use them in the file attachment's filename attribute:

<   Left angle bracket
>   Right angle bracket
\   Backslash
"   Quotation mark
/   Slash mark
:   Colon
|   Vertical bar
?   Question mark
*   Asterisk
    Space

See https://support.microsoft.com/en-us/help/949197/certain-characters-in-a-file-name-may-be-converted-to-underscores-when-a-user-downloads-a-file-by-using-windows-internet-explorer-7

Nick Painter
  • 720
  • 10
  • 13