提问人:sam it 提问时间:8/30/2019 最后编辑:sam it 更新时间:9/4/2019 访问量:455
Vb asp .net webform 下载 excel 文件返回错误的扩展名和文件名
Vb asp .net webform download excel file return the wrong extension and file name
问:
你好
我有我的网络应用程序,您可以下载excel文件。
- 在本地计算机和测试服务器中:文件名返回 具有正确的扩展名和正确的名称,但
在生产服务器中,它返回页面名称的名称
例如:
在我的本地计算机和测试服务器中,我得到 => 报告.xlsx(图 1)
- 生产服务器 我得到 => FileDownloader.aspx (页面名称) (图片 2)
我有 .Net 版本 4.7.2 的 webappilcation
这是我用来返回文件的代码:
Sub BuildHtmlResponse(bytes As Byte(), fileName As String, pFileDownloadType As eFileDownloadType, isCached As Boolean)
Dim encoding As Encoding, contentType As String, charset As String
Select Case pFileDownloadType
Case eFileDownloadType.Pdf
contentType = "application/pdf"
encoding = Encoding.GetEncoding("utf-8")
charset = "utf-8"
Case eFileDownloadType.ExcelXls
contentType = "application/vnd.ms-excel"
encoding = Encoding.GetEncoding("utf-8")
charset = "utf-8"
Case eFileDownloadType.ExcelXlsx
contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
encoding = Encoding.GetEncoding("utf-8")
charset = "utf-8"
Case eFileDownloadType.Html
contentType = "text/html"
encoding = Encoding.GetEncoding("utf-8")
charset = "utf-8"
Case eFileDownloadType.Txt
contentType = "text/plain"
encoding = Encoding.GetEncoding(1252)
charset = "windows-1252"
Case eFileDownloadType.Zip
contentType = "application/zip, application/x-compressed-zip"
encoding = Encoding.GetEncoding("utf-8")
charset = "utf-8"
End Select
With Response
.Clear()
.Buffer = True
.AddHeader("Expires", "0")
If isCached Then
.AddHeader("Pragma", "cache")
End If
.AddHeader("cache-control", "must-revalidate, post-check=0, pre-check=0")
.AddHeader("Accept-Ranges", "bytes")
.AddHeader("content-length", bytes.Length.ToString())
.AddHeader("content-description", "File Transfer")
.AddHeader("content-transfer-encoding", "binary")
.AppendHeader("Content-Disposition", "attachment; filename=" & fileName)
.ContentEncoding = encoding
.ContentType = contentType
.Charset = charset
End With
Try
Response.Flush()
Response.BinaryWrite(bytes)
Finally
Response.End()
End Try
End Sub
先谢谢你
--------更新I-----------------------------------
Protected Sub Button_Download_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button_Download.Click
Dim reportBuilder = ReportBuilder.Build()
Dim reportStream = reportBuilder.GetStream()
BuildHtmlResponse( reportStream.ToArray, reportName + ".xlsx", eFileDownloadType.ExcelXls, False)
End sub
答:
评论