Caroline Dancy
Caroline Dancy

Reputation: 33

File corrupted after downloading

I am receiving the following error while opening a file after downloaded in ASP.NET (VB.NET) - the remote server shared path has files.

The following code used is to download a file:

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

Upvotes: 0

Views: 1024

Answers (1)

ChiragMM
ChiragMM

Reputation: 347

Agree with Wiktor Zychla

Try removing the ContentType of your Response

Response.ContentType = "application/octet-stream"

Upvotes: 1

Related Questions