gpt4 book ai didi

c# - 将 Response.TransmitFile 用于物理文件不起作用

转载 作者:行者123 更新时间:2023-11-30 17:21:02 26 4
gpt4 key购买 nike

我正在尝试使用 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";
}
}

最佳答案

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

关于c# - 将 Response.TransmitFile 用于物理文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3807053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com