这让我非常困惑,因为代码在开发环境中工作并在本地部署到 IIS。但是,当部署到我们的测试服务器上时,纯文本下载的内容会被页面回发替换。要生成的代码;
Protected Sub _uiDownloadLBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles _uiDownloadLBtn.Click
Try
'respond with the export details
RespondWithFile(ExportFilePath)
Catch ex As Exception
'log the exception
_logger.ErrorException("There was a problem trying to download the export file", ex)
lblMessage.Text = "There was a problem trying to download the file"
End Try
End Sub
Public Sub RespondWithFile(ByVal fileName As String)
RespondWithFile(fileName, fileName)
End Sub
Public Sub RespondWithFile(ByVal fileName As String, ByVal downloadFileName As String)
Try
' don't do anything if we have nothing to work with
If String.IsNullOrEmpty(fileName) OrElse Not File.Exists(fileName) Then
Return
End If
Dim fileDetails As New FileInfo(fileName)
Dim response As HttpResponse = HttpContext.Current.Response
response.Clear()
response.AddHeader("Content-Disposition", "attachment; filename=" & Path.GetFileName(downloadFileName))
' strip out the path
response.AddHeader("Content-Length", fileDetails.Length.ToString())
response.ContentType = "text/plain"
response.WriteFile(fileName)
'response.End()
HttpContext.Current.ApplicationInstance.CompleteRequest()
Catch tex As ThreadAbortException
' log as warning and stop it from bubbling back up the call stack
_logger.WarnException("ThreadAbortException thrown", tex)
Thread.ResetAbort()
End Try
End Sub
如您所见,我明确指定了 MIME 类型,text/plain
并且我尝试同时调用response.End()
和HttpContext.Current.ApplicationInstance.CompleteRequest()
。
最初的行为是文件的内容被传输,然后是一个空行,然后是回发。在完全擦除部署的内容并将更新的代码复制到 IIS 服务器后,回发内容将替换下载内容。
逻辑上看的地方是 IIS 实例,但应用程序只使用最基本的设置。IIS 版本为 7,.NET 框架为 2.0。没有设置其他 IIS 配置。
想法?