使用Response.TransmitFile的物理文件不工作 [英] Using Response.TransmitFile for physical file not working

查看:257
本文介绍了使用Response.TransmitFile的物理文件不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用户Response.TransmitFile()来提示下载。
我读了一些关于这个问题的帖子,并根据我的方法了里克施特拉尔的博客
http://www.west-wind.com/weblog/posts/76293.aspx

唯一的区别(即我可以告诉)是我的虚拟目录以外的目标物理文件。
这code被称为一个支持Ajax radgrid控件...我不知道如果response.transmitfile不与Ajax调用工作?
这里是我的code片断:

  //获取文件的物理路径
            字符串docFilePath =(字符串)args.AttachmentKeyValues​​ [DocFilePath];            //创建FileInfo类的新实例获取文件的属性被下载
            FileInfo的文件=新的FileInfo(docFilePath);            //检查文件是否存在
            如果(file.Exists)
            {
                Response.ClearContent();                Response.AddHeader(内容处置,附件;文件名=+ file.Name);
                Response.AddHeader(内容长度,file.Length.ToString());
                Response.ContentType = ReturnExtension(file.Extension.ToLower());
                Response.TransmitFile(file.FullName);                到Response.End();
            }

请参阅系统知道文件存在......它会通过对到Response.End()没有错误...然后继续应用正确......除了没有下载提示。

该ReturnExtension方法是从其他网站提升(抱歉,我不记得在哪里!)如下:

 字符串ReturnExtension(字符串fileExtension)
    {
        //从长远来看这应该在​​一类
        开关(fileExtension)
        {
            案热媒:
            案名.html:
            情况下为.log:
                回报的文/ HTML;
            案名.txt:
                回归text / plain的;
            案例名为.doc:
                返回应用程序/ MS-词;
            案.TIFF:
            案例名为.tif:
                返回到图像/ TIFF;
            案的.asf:
                回归视频/ X-MS-ASF
            案.AVI:
                回归视频/ AVI
            案.ZIP:
                返回应用程序/压缩
            案的.xl​​s:
            案例名为.csv:
                返回应用程序/ vnd.ms-EXCEL;
            案例符.gif:
                返回到图像/ GIF;
            案.JPG:
            案JPEG:
                返回到图像/ JPEG;
            案.BMP:
                返回到图像/ BMP
            案.WAV:
                返回音频/ WAV
            案.MP3:
                返回音频/ MPEG3
            案.MPG:
            案MPEG:
                返回视频/ MPEG
            案的.rtf:
                返回应用程序/ RTF
            案.ASP:
                回报的文/ ASP
            案.PDF:
                返回应用程序/ PDF
            案.fdf:
                返回应用程序/ vnd.fdf
            案.PPT:
                返回应用程序/ mspowerpoint
            案.DWG:
                返回到图像/ vnd.dwg
            案。味精:
                返回应用程序/ msoutlook
            案的.xml:
            案.sdxl:
                返回应用程序/ XML;
            案.xdp:
                返回应用程序/ vnd.adobe.xdp + XML;
            默认:
                返回应用程序/八位字节流;
        }
    }


解决方案

这问题是我无法从一个AJAX调用使Response.TransmitFile()。
阅读一些博客后,我用异步回发来设置一个看不见的iframe的SRC。
在iframe然后发送该文件中加载事件。

I am trying to user the Response.TransmitFile() to prompt a download. I have read a number of posts on the issue and based my method off Rick Strahl's blog http://www.west-wind.com/weblog/posts/76293.aspx

The only difference (that I can tell) is that I am targeting a physical file outside of the virtual directory. This code is called in an ajaxified radgrid... I wonder if the response.transmitfile doesn't work with ajax calls? Here is my code snippet:

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

See the system knows the file exists... it gets through to the Response.End() without error... then continues the app properly... Except there is no download prompt.

The ReturnExtension method is lifted from another site (sorry I can't remember where!) as follows

    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";
        }
    }

解决方案

This issue is that I cannot make Response.TransmitFile() from an AJAX call. After reading a few blogs I use the async postback to set the src of an invisible iframe. The iframe then sends the file in its load event.

这篇关于使用Response.TransmitFile的物理文件不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆