Using Response.TransmitFile for a physical file does not work

I am trying to execute the Response.TransmitFile () command to request a download. I read several posts on this issue and based my method on Rick Strahl's blog http://www.west-wind.com/weblog/posts/76293.aspx

The only difference (I can say) is that I am targeting the physical file outside of the virtual directory. This code is called in ajaxified radgrid ... I wonder if response.transmitfile does not 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 how the system knows that the file exists ... it goes to Response.End () without errors ... then it continues the application correctly ... In addition, there is no invitation to download.

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";
        }
    }
+3
2

, Response.TransmitFile() AJAX. , async postback src iframe. iframe .

+2

, , Transmit, . TransmitFile ? .

0

Source: https://habr.com/ru/post/1766836/


All Articles