문제

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.

다른 팁

I would guess that the code doing the Transmit does not have the rights to open that file. Can you call TransmitFile with an already open file handle? That should work better.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top