在Aap.net中下载文件存在两个问题:
1.文件名超过17更字符时下载文件是文件名的前半部分被截取了或者是几个莫名其妙的字符,如:“我的超长文件名称测试大家看看是什么结果有些出乎意料也很莫名其妙.txt",下载时看到的是”d%ewr看是什么结果有些出乎意料也很莫名其妙.txt"
2.文件名如果出现.时会在后面出现一个[1],如:"1.3数据库设计说明书.doc",下载时看到的是:1[1].3数据库设计说明书.doc"
以上现象在:IE6中出现,在IE8中一切正常。
问题补充:
附上我的代码:
private new static void DownloadFile(HttpResponse res, string filePath, string displayName, bool shouldDelete)
{
if (!File.Exists(filePath))
{
res.Write(FileNotExistsMessage);
}
else
{
FileInfo file = new FileInfo(filePath);
if (String.IsNullOrEmpty(displayName))
{
displayName = HttpUtility.UrlEncode(file.Name);
}
else
{
displayName = HttpUtility.UrlEncode(displayName) + file.Extension;
}
res.Clear();
//res.HeaderEncoding = code;
//res.ContentEncoding = code;
res.AddHeader("Pragma", "public");
res.AddHeader("Expires", "0");
res.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
res.AddHeader("Content-Type", "application/force-download");
res.AddHeader("Content-Type", "application/octet-stream");
res.AddHeader("Content-Type", "application/download");
res.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", displayName));
//log.InfoFormat("编码后的附件名:{0}", displayName);
res.AddHeader("Content-Transfer-Encoding", "binary");
res.AddHeader("Content-Length", file.Length.ToString());
res.WriteFile(filePath);
res.Flush();
if (shouldDelete)
{
File.Delete(filePath);
}
}
}
蓝之风
|
菜鸟二级
|
园豆:
391
提问于:2009-04-29 20:25