【发布时间】:2013-07-16 09:55:22
【问题描述】:
这让我非常困惑,因为代码在开发环境中工作并在本地部署到 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 配置。
想法?
【问题讨论】:
-
文件扩展名是什么?您是否将其 MIME 类型添加到 IIS?
-
文件名为“Orders.txt”。很难变得更简单!
-
是的,“.txt”在 MIME 类型列表中为“text/plain”
-
你试过
response.Write()或response.TransmitFile()吗?response.Write()需要先将文件读入字符串。
标签: vb.net iis-7 download mime