我有一个应用程序,用户可以上传和下载文件,但是在上传和下载文本文件时我遇到了问题。在下载文件后查看文件时,我可以在顶部看到文件的原始内容,但是下面是从下载页面的html内容?
以下是下载的逻辑:
''' <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
有谁知道为什么会这样?
答案 0 :(得分:1)
试试这个
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()
为我工作