HTML附加/替换下载文件的内容 [英] HTML appending/replacing contents of downloaded file

查看:166
本文介绍了HTML附加/替换下载文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是令我困惑的代码在开发环境中工作,并在本地部署到IIS。然而,当部署到我们的测试服务器上时,纯文本下载的内容将被页面回发替换。生成代码

This is puzzling me greatly as the code works in development environment and deployed to IIS locally. However when deployed onto our test server the contents of a plain text download is replaced with the page postback. The code to generate;

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()

As you can see, I am explicitly specifying the MIME type to text/plain and I have tried calling both response.End() and HttpContext.Current.ApplicationInstance.CompleteRequest().

原来的行为是文件的内容正在传输,后面跟着一个空白的li ne然后回发。完全擦除部署的内容并将更新后的代码复制到IIS服务器上,然后回发内容将替换下载内容。

The original behaviour was that the contents of the file were being transmitted followed by a blank line and then the postback. After completly wiping the deployed contents and copying the updated code onto the IIS server the postback contents were then replacing the download contents.

逻辑上要查看的位置将是IIS实例但应用程序只使用最基本的设置。 IIS版本是7,.NET框架是2.0。没有设置其他IIS配置。

Logically the place to look would be the IIS instance but the application uses only the most basic of settings. IIS version is 7 and .NET framework is 2.0. No other IIS configuration was set.

想法?

推荐答案

找到一个解决方案。通过将内容类型缩写为application / pdf,文件扩展名为csv我能够强制文件的内容传递与回发内容分开。这在浏览器中工作,尽管在FireFox中有不幸的影响,它要求用户使用Adobe Reader(application / pdf内容的注册应用程序)打开文本文件。这对我们的客户并不重要,但可能不是别人的选择。

I found "a" solution. By chaning the content type to "application/pdf" and the file extension to ".csv" I was able to force the content delivery of the file be seperated from the postback content. This works across browsers though does have an unfortunate effect in FireFox which asks the user to open the text file using Adobe Reader (the registered application for "application/pdf" content). This doesn't matter for our customer but may not be an option for others.

这确实将IIS中的MIME配置相当牢固地结合在IIS中,仍然困惑于确切的设置导致它。我已经尝试明确地设置txt text / plain csv as text / csv (一个有效但通常是未注册的MIME类型),并且都不起作用。我想知道是否有某种MIME类型的父母/偏好在这里玩。

This does tie the cause down quite firmly in the MIME configuration in IIS though I'm still left puzzled as to the exact setting causing it. I had tried explicitly setting up ".txt" as text/plain and ".csv" as text/csv (a valid but usually unregistered MIME type) and neither worked. I wonder if there's some kind of MIME type heirarchy/preference at play here.

至少有一个可行的解决方案。我希望这能帮助别人。

Still, at least there's a viable solution. I hope this helps others.

这篇关于HTML附加/替换下载文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆