使用Response.TransmitFile使物理文件无法正常工作

我正在尝试使用Response.TransmitFile()来提示下载。 我已经阅读了关于这个问题的一些post,并根据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"; } } 

这个问题是我无法从AJAX调用中创建Response.TransmitFile()。 在阅读了几篇博客后,我使用异步回发来设置不可见iframe的src。 iframe然后在其load事件中发送文件。

我猜想执行传输的代码没有权限打开该文件。 你能用已打开的文件句柄调用TransmitFile吗? 这应该会更好。