【问题标题】:HTML appending/replacing contents of downloaded fileHTML 附加/替换下载文件的内容
【发布时间】: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


【解决方案1】:

我找到了“一个”解决方案。通过将内容类型更改为"application/pdf" 并将文件扩展名更改为".csv",我能够强制将文件的内容传递与回发内容分开。这适用于跨浏览器,但在 FireFox 中确实有一个不幸的效果,它要求用户使用 Adob​​e Reader(“应用程序/pdf”内容的注册应用程序)打开文本文件。这对我们的客户来说无关紧要,但对其他人来说可能不是一个选择。

这确实将原因与 IIS 中的 MIME 配置紧密联系在一起,尽管我仍然对导致它的确切设置感到困惑。我曾尝试将".txt" 显式设置为text/plain,将".csv" 设置为text/csv(一种有效但通常未注册的MIME 类型),但均未成功。我想知道这里是否存在某种 MIME 类型层次结构/偏好。

不过,至少有一个可行的解决方案。我希望这对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-07
    • 2012-08-17
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多