文件扩展名未以 ASP.NET 3.5 形式下载

file extension not downloading in ASP.NET 3.5 forms

我真的很困惑,因为在一个网站上,我的代码可以完美运行,而在另一个网站上却不行。

下载的文件没有扩展名,但是当我重命名下载的文件以包含扩展名(我在文件名中添加 .pdf)时,它可以正确打开为 PDF。我 100% 确定 bytesfilename 是正确的,filenamereport.pdf

原代码如下:

    private void downloadByteStreamAsFile(Byte[] bytes, String fileName)
    {
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.Clear();

            //response.Flush(); //comment this or else no file returned
            response.AddHeader("Content-Type", "binary/octet-stream");
            response.AddHeader("Content-Disposition",
                "attachment; filename=" + fileName + "; size=" + bytes.Length.ToString());

            response.BinaryWrite(bytes);
            response.Flush();
            response.End();

        }
    }

我也试过:

请帮忙

更新:代码在 IE 和 Chrome 中工作正常,只有 Firefox 有丢失文件扩展名的问题

尝试将文件名放在引号中。

response.AddHeader(
    "Content-Disposition",
    "attachment; filename=\"" + fileName + "\"; size=" + bytes.Length.ToString());`

如果可行,那么看看它是否仍然适用于 IE 和 Chrome 以及 Safari。如果不是,请添加 if 语句以有条件地添加引号。

这让我陷入了一个真正的循环,但事实证明只有 PDF 文件没有被正确下载,这是因为设置了 Content-Type。我可能会以不同的方式重构我的代码,但我最喜欢以这种方式编码,因为它对我来说最容易理解和调试。这是我的固定方法:

    private void downloadByteStreamAsFile(Byte[] bytes, String fileName)
    {
        fileName = fileName.Replace(" ", "_");

        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
        response.Clear();

       if( fileName.Contains(".pdf")){
            fileName = HttpUtility.UrlEncode(fileName);
            //response.Flush(); //comment this or else no file returned
            response.AddHeader("Content-Type", "application/pdf");
            response.AddHeader("Content-Disposition",
                "attachment; filename=" + fileName + "; size=" + bytes.Length.ToString());

            response.BinaryWrite(bytes);
            response.Flush();
            response.End();
        } else {
            //response.Flush(); //comment this or else no file returned
            response.AddHeader("Content-Type", "binary/octet-stream");
            response.AddHeader("Content-Disposition",
                "attachment; filename=" + fileName + "; size=" + bytes.Length.ToString());

            response.BinaryWrite(bytes);
            response.Flush();
            response.End();

        }
    }

如果有人将 IIS 7.5 与 Windows Server 2008 R2 一起使用,请遵循以下代码片段...

该代码跨浏览器工作,并为 chrome 浏览器处理 "Network Failed error" > 51 更新

     // CSV Generic List    
         CSVExportGeneric<BookFxDownload> _csv = new CSVExportGeneric<BookFxDownload>(Download);
            // Convert to byte array
                                byte[] a = _csv.ExportToBytes().ToArray();
    // the lines are supposed to be in the same order
                                Response.Clear();
                                Response.Buffer = true;
                                Response.ClearHeaders();
                                Response.ClearContent();

                                //Response.AppendHeader("content-disposition", fileName);
                                Response.ContentType = "application/csv";
                                Response.AddHeader("Content-Length", a.Length.ToString());
//fileName = <yourname>.<extension>
                                Response.AppendHeader("content-disposition", "attachment; filename=" +"\"" + fileName + "\"");
                                Response.Charset = "";
                                Response.OutputStream.Write(a, 0, a.Length);
                                Response.Flush();
                                Response.Close();