C# 对物理文件使用Response.TransmitFile无效
我正在尝试使用Response.TransmitFile()来提示下载。 我已经读了很多关于这个问题的帖子,我的方法是基于瑞克·斯特拉尔的博客 唯一的区别(我可以说)是我的目标是虚拟目录之外的一个物理文件。 此代码在ajaxified radgrid中调用。。。我想知道response.transmitfile是否与ajax调用不兼容? 以下是我的代码片段:C# 对物理文件使用Response.TransmitFile无效,c#,asp.net,response.transmitfile,C#,Asp.net,Response.transmitfile,我正在尝试使用Response.TransmitFile()来提示下载。 我已经读了很多关于这个问题的帖子,我的方法是基于瑞克·斯特拉尔的博客 唯一的区别(我可以说)是我的目标是虚拟目录之外的一个物理文件。 此代码在ajaxified radgrid中调用。。。我想知道response.transmitfile是否与ajax调用不兼容? 以下是我的代码片段: // Get the physical Path of the file string
// 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";
}
}
我猜执行传输的代码没有打开该文件的权限。是否可以使用已打开的文件句柄调用TransmitFile?这应该会更好。这个问题是我无法通过AJAX调用做出响应。TransmitFile()。 在阅读了一些博客之后,我使用异步回发来设置不可见iframe的src。
iframe然后在其加载事件中发送该文件。如果代码没有该文件的权限,file.exist调用不会有问题吗?@Matthew PK:不一定。根据我对transmitfile的记忆,它不会从处理响应的线程发送文件,而是使用异步IO。(底层的东西,比如内核线程打开文件并将数据直接推入套接字)。当你打开一个句柄时,访问检查就完成了,所以如果你能超过这一点,txfile应该能够完成其余的工作。我有类似的问题,你能检查一下吗?我也在我的页面中添加了一个iframe,但一旦设置了iframe src,它就会发回整个页面