下载后文件已损坏

时间:2017-10-13 11:42:37

标签: asp.net vb.net

我在ASP.NET(VB.NET)中下载后打开文件时收到以下错误 - 远程服务器共享路径包含文件。

以下代码用于下载文件:

Private Sub downloadFile(ByVal file As String)

    Try
        Dim requestFile As String = "\\ashleyfurniture\afi-dfs\Arcadia\Vaults\Web\AshleyDirectAttachments\IdeaNetwork\Images" + "\" + file

        If String.IsNullOrEmpty(requestFile) Then
            Throw New FileNotFoundException("File to download cannot be null or empty")
        End If

        ' Get file name from URI string in C#

        Dim uri = New Uri(requestFile)
        Dim filename As String = Path.GetFullPath(uri.OriginalString)
        Dim fileInfo = New FileInfo((uri.OriginalString))

        If Not fileInfo.Exists Then
            Throw New FileNotFoundException("File to download was not found", uri.OriginalString)
        End If


        Response.ContentType = "application/octet-stream"

        Response.AddHeader("Content-Disposition", "attachment; filename=""" + fileInfo.Name + """")
        Response.WriteFile(fileInfo.FullName)
        Response.End()



        ' ignore exception
    Catch generatedExceptionName As ThreadAbortException

    Catch ex As FileNotFoundException
        Response.StatusCode = CInt(System.Net.HttpStatusCode.NotFound)
        Response.StatusDescription = ex.Message
    Catch ex As Exception
        Response.StatusCode = CInt(System.Net.HttpStatusCode.InternalServerError)
        Response.StatusDescription = String.Format("Error downloading file: {0}", ex.Message)
    End Try



End Sub

1 个答案:

答案 0 :(得分:0)

同意Wiktor Zychla

尝试删除回复的ContentType

Response.ContentType = "application/octet-stream"