檔案下載function
Public Function ResponseFile(ByVal FRequest As HttpRequest, ByVal FResponse As HttpResponse, ByVal fileName As String, ByVal fullPath As String, ByVal speed As Long) As Boolean
Try
Dim myFile As FileStream = New FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim br As BinaryReader = New BinaryReader(myFile)
Try
FResponse.AddHeader("Accept-Ranges", "bytes")
FResponse.Buffer = False '關閉存後送出
Dim fileLength As Long = myFile.Length
Dim startBytes As Long = 0
Dim pack As Integer = 10240 '10K bytes
'int sleep = 200; //每秒5次 即5*10K bytes每秒
Dim sleep As Integer = CType(Math.Floor(1000 * pack / speed) + 1, Integer)
If (FRequest.Headers("Range") <> Nothing) Then
FResponse.StatusCode = 206
Dim range() As String = FRequest.Headers("Range").Split(New Char() {"="c, "-"c})
startBytes = Convert.ToInt64(range(1))
End If
FResponse.AddHeader("Content-Length", (fileLength - startBytes).ToString())
If (startBytes <> 0) Then
FResponse.AddHeader("Content-Range", String.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength))
End If
FResponse.AddHeader("Connection", "Keep-Alive")
FResponse.ContentType = "application/octet-stream"
FResponse.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8))
br.BaseStream.Seek(startBytes, SeekOrigin.Begin)
Dim maxCount As Integer = CType(Math.Floor((fileLength - startBytes) / pack) + 1, Integer)
Dim i As Integer
For i = 0 To maxCount - 1 Step i + 1
If (FResponse.IsClientConnected) Then
FResponse.BinaryWrite(br.ReadBytes(pack))
Thread.Sleep(sleep)
Else
i = maxCount
End If
Next
Catch
Return False
Finally
br.Close()
myFile.Close()
End Try
Catch
Return False
End Try
Return True
End Function
調用代碼如下:Try
Dim myFile As FileStream = New FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim br As BinaryReader = New BinaryReader(myFile)
Try
FResponse.AddHeader("Accept-Ranges", "bytes")
FResponse.Buffer = False '關閉存後送出
Dim fileLength As Long = myFile.Length
Dim startBytes As Long = 0
Dim pack As Integer = 10240 '10K bytes
'int sleep = 200; //每秒5次 即5*10K bytes每秒
Dim sleep As Integer = CType(Math.Floor(1000 * pack / speed) + 1, Integer)
If (FRequest.Headers("Range") <> Nothing) Then
FResponse.StatusCode = 206
Dim range() As String = FRequest.Headers("Range").Split(New Char() {"="c, "-"c})
startBytes = Convert.ToInt64(range(1))
End If
FResponse.AddHeader("Content-Length", (fileLength - startBytes).ToString())
If (startBytes <> 0) Then
FResponse.AddHeader("Content-Range", String.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength))
End If
FResponse.AddHeader("Connection", "Keep-Alive")
FResponse.ContentType = "application/octet-stream"
FResponse.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8))
br.BaseStream.Seek(startBytes, SeekOrigin.Begin)
Dim maxCount As Integer = CType(Math.Floor((fileLength - startBytes) / pack) + 1, Integer)
Dim i As Integer
For i = 0 To maxCount - 1 Step i + 1
If (FResponse.IsClientConnected) Then
FResponse.BinaryWrite(br.ReadBytes(pack))
Thread.Sleep(sleep)
Else
i = maxCount
End If
Next
Catch
Return False
Finally
br.Close()
myFile.Close()
End Try
Catch
Return False
End Try
Return True
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim FunName As String = Request("FunName")
Dim TableName As String = Request("TableName")
Dim TablePriKey As String = Request("TablePriKey")
Dim FilePathStr As String = Request("FilePathStr")
Dim id As Integer = Request("id")
Dim sql As String = "select count(*) from WebFileDownCount where FunName='" & FunName & "'"
If Me.ExecString.ExecSQLScalar(sql) = 0 Then
Me.ExecString.ExecSQLNoneQuery("insert into WebFileDownCount (FunName,DownNum) values ('" & FunName & "',1)")
Else
Me.ExecString.ExecSQLNoneQuery("update WebFileDownCount set DownNum=DownNum+1 where FunName='" & FunName & "'")
End If
Dim sql2 = "select " & FilePathStr & " from " & TableName & " where " & TablePriKey & "=" & id
'直接打開
'Dim FileUrl As String = Me.ExecString.ExecSQLScalar(sql2)
'Response.Redirect(Server.UrlEncode(FileUrl))
'作為資料流輸出
Dim fileurl As String = Me.ExecString.ExecSQLScalar(sql2)
Dim filename As String = Right(fileurl, fileurl.Length - fileurl.LastIndexOf("/") - 1)
Page.Response.Clear()
Me.ResponseFile(Page.Request, Page.Response, filename, Server.MapPath(fileurl), 10240)
Page.Response.End()
End Sub
Dim FunName As String = Request("FunName")
Dim TableName As String = Request("TableName")
Dim TablePriKey As String = Request("TablePriKey")
Dim FilePathStr As String = Request("FilePathStr")
Dim id As Integer = Request("id")
Dim sql As String = "select count(*) from WebFileDownCount where FunName='" & FunName & "'"
If Me.ExecString.ExecSQLScalar(sql) = 0 Then
Me.ExecString.ExecSQLNoneQuery("insert into WebFileDownCount (FunName,DownNum) values ('" & FunName & "',1)")
Else
Me.ExecString.ExecSQLNoneQuery("update WebFileDownCount set DownNum=DownNum+1 where FunName='" & FunName & "'")
End If
Dim sql2 = "select " & FilePathStr & " from " & TableName & " where " & TablePriKey & "=" & id
'直接打開
'Dim FileUrl As String = Me.ExecString.ExecSQLScalar(sql2)
'Response.Redirect(Server.UrlEncode(FileUrl))
'作為資料流輸出
Dim fileurl As String = Me.ExecString.ExecSQLScalar(sql2)
Dim filename As String = Right(fileurl, fileurl.Length - fileurl.LastIndexOf("/") - 1)
Page.Response.Clear()
Me.ResponseFile(Page.Request, Page.Response, filename, Server.MapPath(fileurl), 10240)
Page.Response.End()
End Sub
申明
非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!
博文欢迎转载,但请给出原文连接。