下载文件 文件名 乱码的问题.
之前看到博友们一些文章解决asp.net的下载文件名的乱码,但是貌似针对其他浏览器比如Firefox的中文还是有乱码,空格出现+号(这时候IE,Chrome没问题)
刚才试了试,用这个方法可以搞定了IE,Chrome,Firefox.其他没测试,先贴上代码:
代码
public class UrlEncoder
{
/// <summary>
/// Encodes non-US-ASCII characters in a string.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ToHexString(string s)
{
char[] chars = s.ToCharArray();
StringBuilder builder = new StringBuilder();
for (int index = 0; index < chars.Length; index++)
{
bool needToEncode = NeedToEncode(chars[index]);
if (needToEncode)
{
string encodedString = ToHexString(chars[index]);
builder.Append(encodedString);
}
else
{
builder.Append(chars[index]);
}
}
return builder.ToString();
}
/// <summary>
/// Determines if the character needs to be encoded.
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static bool NeedToEncode(char chr)
{
string reservedChars = "$-_.+!*'(),@=&";
if (chr > 127)
return true;
if (char.IsLetterOrDigit(chr) || reservedChars.IndexOf(chr) >= 0)
return false;
return true;
}
/// <summary>
/// Encodes a non-US-ASCII character.
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static string ToHexString(char chr)
{
UTF8Encoding utf8 = new UTF8Encoding();
byte[] encodedBytes = utf8.GetBytes(chr.ToString());
StringBuilder builder = new StringBuilder();
for (int index = 0; index < encodedBytes.Length; index++)
{
builder.AppendFormat("%{0}", Convert.ToString(encodedBytes[index], 16));
}
return builder.ToString();
}
}
{
/// <summary>
/// Encodes non-US-ASCII characters in a string.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ToHexString(string s)
{
char[] chars = s.ToCharArray();
StringBuilder builder = new StringBuilder();
for (int index = 0; index < chars.Length; index++)
{
bool needToEncode = NeedToEncode(chars[index]);
if (needToEncode)
{
string encodedString = ToHexString(chars[index]);
builder.Append(encodedString);
}
else
{
builder.Append(chars[index]);
}
}
return builder.ToString();
}
/// <summary>
/// Determines if the character needs to be encoded.
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static bool NeedToEncode(char chr)
{
string reservedChars = "$-_.+!*'(),@=&";
if (chr > 127)
return true;
if (char.IsLetterOrDigit(chr) || reservedChars.IndexOf(chr) >= 0)
return false;
return true;
}
/// <summary>
/// Encodes a non-US-ASCII character.
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static string ToHexString(char chr)
{
UTF8Encoding utf8 = new UTF8Encoding();
byte[] encodedBytes = utf8.GetBytes(chr.ToString());
StringBuilder builder = new StringBuilder();
for (int index = 0; index < encodedBytes.Length; index++)
{
builder.AppendFormat("%{0}", Convert.ToString(encodedBytes[index], 16));
}
return builder.ToString();
}
}
然后在下载下面中:
代码
response.ContentType = "application/octet-stream";
response.HeaderEncoding = Encoding.UTF8;
string encodefileName = UrlEncoder.ToHexString(fileName);
string userAgent = request.UserAgent.ToUpper();
string fileNameHead = null;
if (userAgent.IndexOf("FIREFOX") > 0)
fileNameHead = string.Format("attachment;filename*=utf8''{0}", encodefileName);
else
fileNameHead = string.Format("attachment;filename={0}", encodefileName);
response.AddHeader("Content-Disposition", fileNameHead);
response.HeaderEncoding = Encoding.UTF8;
string encodefileName = UrlEncoder.ToHexString(fileName);
string userAgent = request.UserAgent.ToUpper();
string fileNameHead = null;
if (userAgent.IndexOf("FIREFOX") > 0)
fileNameHead = string.Format("attachment;filename*=utf8''{0}", encodefileName);
else
fileNameHead = string.Format("attachment;filename={0}", encodefileName);
response.AddHeader("Content-Disposition", fileNameHead);
就成了.