Question

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

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

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.

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.

Ideas?

Was it helpful?

Solution

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top