在Chrome中下载简单的text / csv文件失败

时间:2014-03-02 20:11:26

标签: asp.net .net vb.net google-chrome webforms

我正在尝试找出我们在下载时遇到的问题,问题似乎只存在于Chrome中,仅适用于txt和csv文件。下载zip文件的工作方式与预期一致。 IE和Firefox适用于所有场景。 出于某种原因,Chrome会在未正确设置名称的情况下下载txt和csv文件。文件继承了从中下载的页面的名称,并且没有扩展名(see screenshot)。

我有一个简单的Webform,只有3个链接按钮,后面是一个相当简单的代码,为了这个问题,我简化了这个代码。

Imports System.IO

Public Class _Default
    Inherits Page

    Private Const FILE_PATH_CSV As String = "D:\Temp\Download\testFile.csv"
    Private Const FILE_PATH_ZIP As String = "D:\Temp\Download\testFile.zip"
    Private Const FILE_PATH_TXT As String = "D:\Temp\Download\testFile.txt"

    Private Sub btnDownloadCSV_Click(sender As Object, e As EventArgs) Handles btnDownloadCSV.Click
        Dim file As New FileInfo(FILE_PATH_CSV)
        If Not file.Exists() Then
            Throw New Exception("No file")
        End If
        Try
            DownloadFile(file)
        Catch ex As Exception
            'TODO: Log!
        End Try
    End Sub

    Private Sub btnDownloadTXT_Click(sender As Object, e As EventArgs) Handles btnDownloadTXT.Click
        Dim file As New FileInfo(FILE_PATH_TXT)
        If Not file.Exists() Then
            Throw New Exception("No file")
        End If
        Try
            DownloadFile(file)
        Catch ex As Exception
            'TODO: Log!
        End Try
    End Sub

    Private Sub btnDownloadZIP_Click(sender As Object, e As EventArgs) Handles btnDownloadZIP.Click
        Dim file As New FileInfo(FILE_PATH_ZIP)
        If Not file.Exists() Then
            Throw New Exception("No file")
        End If
        Try
            DownloadFile(file)
        Catch ex As Exception
            'TODO: Log!
        Finally
            With Response
                .Flush()
                .End()
            End With
        End Try
    End Sub

    Private Sub DownloadFile(ByVal file As FileInfo)
        With Response
            .Clear()
            .ClearHeaders()
            .ClearContent()
            .Buffer = True

            Response.ContentType = GetContentType(file)
            Response.AddHeader("Content-Disposition:", String.Format("attachment;filename=""{0}""", file.Name.ToString()))
            Response.AddHeader("Content-Length", file.Length.ToString())
            Response.TransmitFile(file.FullName)
        End With
    End Sub

    Private Function GetContentType(ByVal file As FileInfo)
        Select Case file.Extension.ToLower()
            Case ".txt", ".csv"
                Return "application/octet-stream"
            Case ".zip"
                Return "application/x-zip-compressed"
            Case Else
                Return "application/octet-stream"
        End Select
    End Function
End Class

我尝试将内容类型切换为“text / plain”或其他选项,因为我在网上搜索答案但没有运气。我觉得我错过了一些东西,但我找不到任何能在网上回答我问题的东西。 有没有人有任何想法为什么会这样?

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您需要将MIME类型更改为correct values

        Case ".csv"
            Return "text/csv"
        Case ".txt"
            Return "text/plain"

您可能还需要更改从

发送文件的方法
        Response.TransmitFile(file.FullName)

        Response.WriteFile(file.FullName, False)

MSDN上的文档并不清楚这两种方法之间的区别。我从未使用TransmitFile,但已成功使用WriteFile

答案 1 :(得分:0)

问题解决了。我想我应该更加小心复制/粘贴,因为这导致我2天非常沮丧的调试。

Response.AddHeader("Content-Disposition:", String.Format("attachment;filename=""{0}""", file.Name.ToString()))

应该是

Response.AddHeader("Content-Disposition", String.Format("attachment;filename=""{0}""", file.Name.ToString()))

没有必要使用额外的冒号,当我取出它时 - 一切正常! 感谢所有帮助过的人!