Danish characters are replaced in CSV - asp.net

My code to create a CSV file that contains Danish characters
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/csv;charset=utf-8";
Response.Charset = "utf-8";
Response.Write(sb.ToString());
Response.End();
Don't know why but when result CSV is created danish characters are replaced with some special characters like ??j.Can any one give me any clue?

Is the source text in the StringBuilder UTF-8? If not there is a chance that .NET gets confused and writes the wrong characters. If it isn't in UTF-8 try running Encoding.Convert before writing to the client.

Related

Japenese character in a filename changes to garbage if I download file from IE 11

If I download a file which has Japanese character as its file name, so it gets to some garbage language in IE 11.
I found the solution, here it is, I used UrlEncode on the filename which helped me solve my problem.
Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", HttpUtility.UrlEncode(docFileDTO.FileName)));

TransmitFile with filename containing spaces

In C# ASP.Net Website, to transfer the file to client I am using
String file_path = Server.MapPath("~/files/"+file_name);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + file_Name);
HttpContext.Current.Response.TransmitFile(file_path);
HttpContext.Current.Response.End();
It is working perfectly, but when the file name contains any spaces the downloaded file has a name only up to the first word. For ex: If my file name is "This is demo.txt" then the downloaded file name becomes "This" with no extension. Hence the user downloading it is not able to identify its type.
How can we avoid happening it for file name containing spaces?
I tried using
String file_path = "'"+Server.MapPath("~/files/"+file_name)+"'";
But it didn't work.
Also its not possible for me to replace ( with '_' or '-') or remove all the spaces present in the file name which are present on the server.
You should enclose the filename in quotes.
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + file_Name + "\"");
And if you are using VS2015 or later, you can make it a bit tidier with string interpolation:
HttpContext.Current.Response.AddHeader("Content-Disposition", $"attachment;filename=\"{file_Name}\"");
Also, the filename in the header does not have to be the same as the name of the file. The filename in the header is only a suggestion to the user.
For full details, see RFC 6266 "Use of the Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)," especially with regard to which characters may cause problems in a filename.

FSO OpenTextFile with french characters

Using ASP's file system object (FSO), I'm trying to read a txt file with OpenTextFile that contains French characters (e and a with accents for e.g). Those characters come out wrong.
I tried specifying the format to TristateTrue to open the file as Unicode but to no avail.
I've been reading about using the ADO Stream object instead but I hoped there would be a way with FSO. Does anyone have any ideas?
Most likely the file is saved in UTF-8 encoding. The FileSystemObject does not handle UTF-8.
Either have the file saved as Unicode or use the ADODB.Stream object. The ADODB.Stream has a LoadFromFile method and does support UTF-8.
Dim s
Dim stream : Set stream = CreateObject("ADODB.Stream")
stream.CharSet = "UTF-8"
stream.LoadFromFile Server.MapPath("yourfile.txt")
s = stream.ReadAll
stream.Close

ASP.NET Export to Excel, if filename contains character # or /, it is converted into underscore(_) while poping-up the open/save dialog!

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!
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.)
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

Output Response in ANSI Characters format in ASP.net

I am writing the data to the output browser using Response.write(some byte arrary)
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.OutputStream.Write(report, 0, report.Length);
Response.Flush();
Response.Close();
In my file I am having ANSI characters I need to write the information in the format ANSI when
i open my excel file.
Do we need to add anything.
I have encoded it to ANSI but its not working.
You say you're writing a byte array, but there's no such thing as an "ANSI" byte. A byte is a byte is a byte. You only need ANSI or any other encoding if you're writing strings or chars.
So if your report is a byte[] representing ANSI text, how have you encoded it?

Resources