Srb1313711
Srb1313711

Reputation: 2047

Files downloading with page content

I have an application in which a user is able to upload and download files, however when uploading and downloading text files im getting an issue. When viewing the file after it has downloaded i can see the original contents of the file at the top but then below that is the html content of the page it was downloaded from?

Here is the logic for the download:

''' <summary>
''' Downloads a file to the browser
''' </summary>
''' <param name="serverFilePath"></param>
Public Sub DownloadFile(serverFilePath As String)

    Dim fileName As String = Path.GetFileName(serverFilePath)
    Dim ext As String = Path.GetExtension(serverFilePath)

    Dim context As HttpContext = HttpContext.Current
    Dim response As HttpResponse = context.Response
    response.BufferOutput = True
    response.Clear()
    'Returns the mime type for the given extension
    response.ContentType = MimeTypeHelper.GetMimeType(ext)
    response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";")
    response.TransmitFile(serverFilePath)
    response.Flush()
    context.ApplicationInstance.CompleteRequest()

End Sub

Does anyone have any idea why this might be happening?

Upvotes: 0

Views: 83

Answers (1)

Nickthename
Nickthename

Reputation: 259

Try this

response.ClearContent()
response.ClearHeaders()
response.AppendHeader("content-length", bytes.Length.ToString())
response.AppendHeader("Content-Disposition","attachment; filename=" + fileName + ";")
response.ContentType = MimeTypeHelper.GetMimeType(ext)
response.BinaryWrite(bytes) 'Where bytes is ann array of bytes representing the file
response.Flush()
response.Close()

Works for me

Upvotes: 1

Related Questions