2013-07-17 32 views
1

這讓我非常困惑,因爲代碼在開發環境中工作並在本地部署到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配置。

想法?

+1

什麼是擴展名?你把它的'MIME類型添加到IIS? – basher

+0

文件名是「Orders.txt」。很難變得更簡單! – DiskJunky

+0

是的,「.txt」在MIME類型列表中爲「text/plain」 – DiskJunky

回答

0

我找到了「a」解決方案。通過將內容類型改爲"application/pdf",文件擴展名爲".csv",我能夠強制從回發內容中分離文件的內容。這適用於各種瀏覽器,雖然在FireFox中有不良影響,它要求用戶使用Adobe Reader(「application/pdf」內容的註冊應用程序)打開文本文件。這對我們的客戶無關緊要,但可能不適用於其他人。

這確實將原因與IIS中的MIME配置緊密聯繫在一起,儘管我仍然對造成它的確切設置感到困惑。我試圖明確地設置".txt"text/plain".csv"text/csv(一個有效的但通常是未註冊的MIME類型),都沒有工作。我想知道在這裏玩是否有某種MIME類型的heirarchy/preference。

不過,至少有一個可行的解決方案。我希望這可以幫助別人。