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

时间:2014-07-16 09:27:40

标签: c# asp.net iis download

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

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

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

我在localhost(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";
            }
        }

1 个答案:

答案 0 :(得分:0)

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

方便的链接:

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