我正在尝试使用 Response.TransmitFile() 来提示下载。我已经阅读了许多关于这个问题的帖子,并基于 Rick Strahl 的博客 http://www.west-wind.com/weblog/posts/76293.aspx我的方法
唯一的区别(我可以说)是我的目标是虚拟目录之外的物理文件。此代码在 ajaxified radgrid 中调用...我想知道 response.transmitfile 是否不适用于 ajax 调用?这是我的代码片段:
// Get the physical Path of the file
string docFilePath = (string)args.AttachmentKeyValues["DocFilePath"];
// Create New instance of FileInfo class to get the properties of the file being downloaded
FileInfo file = new FileInfo(docFilePath);
// Checking if file exists
if (file.Exists)
{
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = ReturnExtension(file.Extension.ToLower());
Response.TransmitFile(file.FullName);
Response.End();
}
看到系统知道文件存在...它通过 Response.End() 没有错误...然后继续正确地应用程序...除了没有下载提示。
ReturnExtension 方法是从另一个站点提取的(抱歉我不记得在哪里!)如下
string ReturnExtension(string fileExtension)
{
// In the long run this should go in a class
switch (fileExtension)
{
case ".htm":
case ".html":
case ".log":
return "text/HTML";
case ".txt":
return "text/plain";
case ".doc":
return "application/ms-word";
case ".tiff":
case ".tif":
return "image/tiff";
case ".asf":
return "video/x-ms-asf";
case ".avi":
return "video/avi";
case ".zip":
return "application/zip";
case ".xls":
case ".csv":
return "application/vnd.ms-excel";
case ".gif":
return "image/gif";
case ".jpg":
case "jpeg":
return "image/jpeg";
case ".bmp":
return "image/bmp";
case ".wav":
return "audio/wav";
case ".mp3":
return "audio/mpeg3";
case ".mpg":
case "mpeg":
return "video/mpeg";
case ".rtf":
return "application/rtf";
case ".asp":
return "text/asp";
case ".pdf":
return "application/pdf";
case ".fdf":
return "application/vnd.fdf";
case ".ppt":
return "application/mspowerpoint";
case ".dwg":
return "image/vnd.dwg";
case ".msg":
return "application/msoutlook";
case ".xml":
case ".sdxl":
return "application/xml";
case ".xdp":
return "application/vnd.adobe.xdp+xml";
default:
return "application/octet-stream";
}
}