使用Response.TransmitFile的物理文件不工作
-
25-09-2019 - |
题
我试图用户Response.TransmitFile()来提示下载。 我读了一些关于这个问题的帖子,并根据我的方法关闭里克施特拉尔的博客 http://www.west-wind.com/weblog/posts/76293.aspx
唯一的区别(即我可以告诉)是,我靶向虚拟目录的物理文件之外。 这段代码被称为在一个支持Ajax 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然后发送该文件在其加载事件。
其他提示
我猜想,做发射的代码没有打开该文件的权限。你可以调用的TransmitFile与一个已经打开的文件句柄?这应该更好地工作。
不隶属于 StackOverflow