今天測試文件下載程序中發現的文件名過長的問題
居然發現文件名編碼後長度超過
就會不能正確顯示和下載
最後只好找了這樣一個折中的方法
截短了
下面是那裡的代碼
/// <summary>
/// 下載附件
/// </summary>
/// <param name=
fileName
>文件名</param>
/// <param name=
path
>文件路徑</param>
public static void DownLoadFileAttachment(string fileName
string path)
{
if (System
IO
File
Exists(path))
{
try
{
fileName = fileName
Trim();
for (int i =
; i < System
IO
Path
InvalidPathChars
Length ; i ++)
{
fileName = fileName
Trim()
Replace(System
IO
Path
InvalidPathChars[i]
ToString()
string
Empty);
}
fileName = fileName
Replace(System
IO
Path
PathSeparator
ToString()
string
Empty);
int maxLength =
;
int length = HttpUtility
UrlEncode(fileName)
Length;
while (length > maxLength)
{
int index = fileName
LastIndexOf(
);
if (index >
)
{
fileName = fileName
Substring(
index
) + fileName
Substring(index);
}
else
{
fileName = fileName
Substring(
fileName
Length
);
}
length = HttpUtility
UrlEncode(fileName)
Length;
}
System
IO
FileInfo file = new System
IO
FileInfo(path);
HttpContext
Current
Response
Clear();
HttpContext
Current
Response
AppendHeader(
Content
Disposition
attachment; filename=
+ HttpUtility
UrlEncode(fileName));
HttpContext
Current
Response
AppendHeader(
Content
Length
file
Length
ToString());
HttpContext
Current
Response
ContentType =
application/octet
stream
;
HttpContext
Current
Response
WriteFile(file
FullName);
HttpContext
Current
Response
End();
}
catch
{
}
}
else
{
HttpContext
Current
Response
Clear();
DisplayNoFileMessage();
HttpContext
Current
Response
End();
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27168.html