简体   繁体   English

使用ASP.net C#下载文件时出错

[英]Error while downloading file using ASP.net c#

Below is code where AJAX send POST request 下面是AJAX发送POST请求的代码

Basically I want to download a file from some location(full path) on page load 基本上我想在页面加载时从某个位置(完整路径)下载文件

But when I this page is called no error is thrown page get call succesfully Even I get file.Exist=true , Response is created but on browser nothing happens I expecting file download dialog. 但是,当我调用此页面时,没有错误引发页面成功获得调用即使我得到了file.Exist = true,也创建了响应,但是在浏览器上什么也没发生,我期望文件下载对话框。 on further debugging I found exception in Response object Headers = 'Response.Headers' threw an exception of type 'System.PlatformNotSupportedException' This operation requires IIS integrated pipeline mode 在进一步调试时,我在Response对象Headers ='Response.Headers'中发现异常, 引发了类型为'System.PlatformNotSupportedException'的异常。 此操作需要IIS集成管道模式

I am running this web page in my localhost(visual studio development server). 我正在本地主机(Visual Studio开发服务器)中运行此网页。 .net4 and VS2010 .net4和VS2010

On Page Load Code: 页面加载代码:

  protected void Page_Load(object sender, EventArgs e)
    {


        string path = Request.Form["path"];

       // string reportName=path.Substring( path.LastIndexOf("\\") ,path.Length);
        DAL.IO_helper i = new DAL.IO_helper();
        string fullpath = i.GetReportsPath() + path;

        FileInfo report = new FileInfo(fullpath);

        if (report.Exists)
        {


            Response.ClearContent();

            Response.ClearHeaders();
            // Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
            Response.AddHeader("Content-Disposition", "attachment; filename=" + report.Name);

            // Add the file size into the response header
            Response.AddHeader("Content-Length", report.Length.ToString());

            // Set the ContentType
            Response.ContentType = ReturnExtension(report.Extension.ToLower());

            // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
            Response.TransmitFile(report.FullName);

            //Transmit file

            Response.Flush();

            // End the response
            Response.End();

        }
        else
        {
            Response.ClearContent();
            // End the response
            Response.End();
        }


    }

function 功能

 private string ReturnExtension(string fileExtension)
        {
            switch (fileExtension)
            {

                case ".txt":
                    return "text/plain";
                case ".dat":
                    return "text/plain";
                case ".doc":
                    return "application/ms-word";
                case ".docx":
                    return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                case ".zip":
                    return "application/zip";
                case ".xls":
                    return "application/vnd.ms-excel";
                case ".xlsx":
                    return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                case ".csv":
                    return "application/vnd.ms-excel";
                case ".pdf":
                    return "application/pdf";
                case ".xml":
                    return "application/xml";
                case ".sdxl":
                    return "application/xml";
                default:
                    return "application/octet-stream";
            }
        }

The short answer is, you can't do this with the WebDev development web server since it doesn't support the integrated pipeline mode. 简短的答案是,由于WebDev开发Web服务器不支持集成管道模式,因此您无法执行此操作。 The solution is to use IIS Express to host the page. 解决方案是使用IIS Express托管页面。 You still get full debugging integration and so on. 您仍然可以获得完整的调试集成,依此类推。

Handy links: 方便的链接:

Or just google VS2010 IIS Express for any number of other helpful hints. 或只是google VS2010 IIS Express以获取许多其他有用的提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM