代码之家  ›  专栏  ›  技术社区  ›  Matthew

使用响应.传输文件对于物理文件不工作

  •  3
  • Matthew  · 技术社区  · 15 年前

    我读了很多关于这个问题的文章,我的方法是基于rickstrahl的博客 http://www.west-wind.com/weblog/posts/76293.aspx

    下面是我的代码片段:

                // 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();
                }
    

    看系统知道文件存在。。。它通过了回应。结束()没有错误。。。然后继续正确的应用程序。。。只是没有下载提示。

    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";
            }
        }
    
    2 回复  |  直到 15 年前
        1
  •  2
  •   Matthew    15 年前

    这个问题我无法回答响应.传输文件()来自AJAX调用。 在阅读了一些博客之后,我使用async postback来设置不可见iframe的src。 然后iframe在其load事件中发送文件。

        2
  •  0
  •   user180326 user180326    15 年前

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

    推荐文章