ASP.NET コードC#で、
CSVファイルを生成しダウンロードできるようにしたのですが、
日本語ファイル名が文字化けする!!
調べてみると、なるほど、
Microsoftが既にそうゆう事象があることを言っているんですね。
●ファイルをダウンロードする ASP.NET ページで日本語ファイル名が文字化けする
http://support.microsoft.com/kb/436616/ja
そしてその対応をしてみました。
が!!><
むー!
ファイル名は文字化けしたままでした。
●サンプルソース
protected void ButtonCSV_Click(object sender, EventArgs e)
{
string fileName = "テスト.csv";
string content = "内容";
Response.ContentType = "application/download";
Response.HeaderEncoding = System.Text.Encoding.GetEncoding("Shift_JIS");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("Shift_JIS");
Response.AppendHeader("Content-Disposition", "Attachment; filename=" + fileName);
Response.Write(content);
Response.End();
}
"てすと.csv" → "縺ヲ縺吶→.csv"
"テスト.csv" → "繝・せ繝・csv"
"あ.csv" → "縺・csv"
"ああ.csv" → "縺ゅ≠.csv"
"ああああ.csv" → "縺ゅ≠縺ゅ≠.csv"
という風になります。
内容の文字コードは"Shift_JIS"ですが、ファイル名は"UTF-8"のままだと思われます。
正確な原因はわかりませんが、上記の解決方法で解決できなかったのは、
・IE9であること
が影響しているのかなぁと思いました。
そこで、別の方法で、下記のようにしたら、うまく日本語表記できました。
●サンプルソース
protected void ButtonCSV_Click(object sender, EventArgs e)
{
string fileName = "テスト.csv";
string content = "内容";
Response.ContentType = "application/download";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("Shift_JIS");
Response.AppendHeader("Content-Disposition", "Attachment; filename=" + HttpUtility.UrlEncode(fileName));
Response.Write(content);
Response.End();
}
HttpUtility.UrlEncode(fileName) がポイントですね。
0 件のコメント:
コメントを投稿