【问题标题】:Error while downloading file using ASP.net c#使用 ASP.net c# 下载文件时出错
【发布时间】:2014-07-16 09:27:39
【问题描述】:

下面是 AJAX 发送 POST 请求的代码

基本上我想在页面加载时从某个位置(完整路径)下载文件

但是当我调用这个页面时没有抛出错误页面成功调用 即使我得到 file.Exist=true ,也会创建响应但在浏览器上没有任何反应 我期待文件下载对话框。在进一步调试中,我在 Response 对象中发现异常 Headers = 'Response.Headers' 引发了“System.PlatformNotSupportedException”类型的异常 此操作需要 IIS 集成管道模式

我在我的本地主机(Visual Studio 开发服务器)中运行这个网页。 .net4 和 VS2010

页面加载代码:

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


    }

功能

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

【问题讨论】:

标签: c# asp.net iis download


【解决方案1】:

简短的回答是,您不能使用 WebDev 开发 Web 服务器执行此操作,因为它不支持集成管道模式。解决方案是使用 IIS Express 来托管页面。您仍然可以获得完整的调试集成等。

方便的链接:

或者只是谷歌VS2010 IIS Express 以获得任何其他有用的提示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多