函數名:ResponseFile
功能 :用戶端從伺服器端下載一個檔案
傳回值:返回True表示伺服器響應成功,返回False表示失敗
參數 :
PageResponse 響應用戶端的Response對象,用Page.Response引用
DownloadFileName 用戶端下載檔案的檔案名稱
LocalFilePath 伺服器端待下載檔案的路徑
DownloadBuffer 伺服器端讀取檔案的緩衝區大小,單位為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 |