這讓我非常困惑,因爲代碼在開發環境中工作並在本地部署到IIS。但是,當部署到我們的測試服務器上時,純文本下載的內容將被頁面回發代替。要生成的代碼;HTML添加/替換下載文件的內容
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? – basher
文件名是「Orders.txt」。很難變得更簡單! – DiskJunky
是的,「.txt」在MIME類型列表中爲「text/plain」 – DiskJunky