Thursday, March 22, 2012

Working with Unicode File Names For Download

Well, what im trying to do is to download a page as an excel file onto the client. The file name used is made up of Japanese characters (which I hard coded into the code during the setting up of Response.addheader).
When i try to download the file, the download dialog comes up but the file name is garbage. :(

Response.ContentType = "application/vnd.ms-excel;charset=X-SJIS"
Response.AddHeader("Content-Disposition", "attachment;filename=店長在籍者リスト.xls")



With other scripting languages the solution was to convert encodings. and for this i used this one

Response.AddHeader("Content-Disposition", "attachment;filename=" + Encoding.GetEncoding("Shift_JIS").GetString(Encoding.Unicode.GetBytes("店長在籍者リスト")) + ".xls")



still won't work.

Please help. :cry:After much research, I finally found the solution to my ASP.NET problem, im posting the solution here so that i may in the future reference this one.

Details of the said solution can be found in this site.
http://www.codeproject.com/aspnet/NonUSASCII.asp?et=You+must+be+signed+in+to+vote

Dim strData As String

strData = "店長在籍者リスト"

Dim utf8 As New UTF8Encoding
Dim strBldr As New StringBuilder

Dim encodedBytes As Byte()
Dim charData As Char()

charData = strData.ToCharArray

Dim i As Integer, j As Integer

For i = 0 To charData.Length - 1
encodedBytes = Encoding.UTF8.GetBytes(charData(i))
For j = 0 To encodedBytes.Length - 1
strBldr.AppendFormat("%{0}", Convert.ToString(encodedBytes(j), 16))
Next
Next

Response.ContentType = "application/vnd.ms-excel;charset=X-SJIS"
Response.AddHeader("Content-Disposition", "attachment;filename=" + strBldr.ToString + ".xls")
I could've easily told you that. :p

0 comments:

Post a Comment