我正在尝试使用 Response.TransmitFile() 来提示下载。
我读过很多关于这个问题的帖子,并根据 Rick Strahl 的博客制定了我的方法http://www.west-wind.com/weblog/posts/76293.aspx 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";
}
}
这个问题是我无法通过 AJAX 调用进行 Response.TransmitFile() 。
阅读了一些博客后,我使用异步回发来设置不可见 iframe 的 src。
然后 iframe 在其加载事件中发送该文件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)