文件扩展名未以 ASP.NET 3.5 形式下载
file extension not downloading in ASP.NET 3.5 forms
我真的很困惑,因为在一个网站上,我的代码可以完美运行,而在另一个网站上却不行。
下载的文件没有扩展名,但是当我重命名下载的文件以包含扩展名(我在文件名中添加 .pdf
)时,它可以正确打开为 PDF。我 100% 确定 bytes
和 filename
是正确的,filename
是 report.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();
}
}
我也试过:
- 使用输出流Trying to stream a PDF file with asp.net is producing a "damaged file"
- 删除
Content-Disposition
中分号后的 space file extensions lost between browsers in asp.net c# application
"attachment; filename=" + fileName + ".PDF; size=" + bytes.Length.ToString());
(所以现在文件应该命名为 report.pdf.PDF
但它仍然只命名为 report
)
请帮忙
更新:代码在 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();
我真的很困惑,因为在一个网站上,我的代码可以完美运行,而在另一个网站上却不行。
下载的文件没有扩展名,但是当我重命名下载的文件以包含扩展名(我在文件名中添加 .pdf
)时,它可以正确打开为 PDF。我 100% 确定 bytes
和 filename
是正确的,filename
是 report.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();
}
}
我也试过:
- 使用输出流Trying to stream a PDF file with asp.net is producing a "damaged file"
- 删除
Content-Disposition
中分号后的 space file extensions lost between browsers in asp.net c# application "attachment; filename=" + fileName + ".PDF; size=" + bytes.Length.ToString());
(所以现在文件应该命名为report.pdf.PDF
但它仍然只命名为report
)
请帮忙
更新:代码在 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();