ASP.NET中完成直接從頁面上下載文件,而不須引用文件URL來下載
發(fā)表時間:2024-06-02 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]函數(shù)名:ResponseFile功能 :客戶端從服務(wù)器端下載一個文件返回值:返回True表示服務(wù)器響應(yīng)成功,返回False表示失敗參數(shù) : PageResponse 響應(yīng)客戶端的Response對象,用Page.Response引用 DownloadFi...
函數(shù)名:ResponseFile
功能 :客戶端從服務(wù)器端下載一個文件
返回值:返回True表示服務(wù)器響應(yīng)成功,返回False表示失敗
參數(shù) :
PageResponse 響應(yīng)客戶端的Response對象,用Page.Response引用
DownloadFileName 客戶端下載文件的文件名
LocalFilePath 服務(wù)器端待下載文件的路徑
DownloadBuffer 服務(wù)器端讀取文件的緩沖區(qū)大小,單位為KB
Public Function ResponseFile(ByRef PageResponse As HttpResponse, ByVal DownloadFileName As String, ByVal LocalFilePath As String, ByVal DownloadBuffer As Long) As Boolean
Dim Reader As System.IO.FileStream
Dim Buffer() As Byte
Dim FileLength As Long
Dim FileBuffer As Long = 1024 * DownloadBuffer
Dim ReadCount As Long
ReadCount = FileBuffer
ReDim Buffer(ReadCount - 1)
Try
Reader = System.IO.File.OpenRead(LocalFilePath)
FileLength = Reader.Length
Try
PageResponse.Buffer = False
PageResponse.AddHeader("Connection", "Keep-Alive")
PageResponse.ContentType = "application/octet-stream"
PageResponse.AddHeader("Content-Disposition", "attachment;filename=" + DownloadFileName)
PageResponse.AddHeader("Content-Length", FileLength.ToString)
While ReadCount = FileBuffer
ReadCount = Reader.Read(Buffer, 0, FileBuffer)
ReDim Preserve Buffer(ReadCount - 1)
PageResponse.BinaryWrite(Buffer)
End While
Response.End()
Catch ex As Exception
Return False
Finally
Reader.Close()
End Try
Catch ex As Exception
Return False
End Try
Return True
End Function