檔案下載function

        Public Function ResponseFile(ByVal FRequest As HttpRequest, ByVal FResponse As HttpResponse, ByVal fileName As StringByVal fullPath As StringByVal speed As LongAs 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) + 1Integer)
                    
If (FRequest.Headers("Range"<> NothingThen
                        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 <> 0Then
                        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) + 1Integer)
                    
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 ObjectByVal 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
posted @ 2006-12-02 15:01  Athrun  阅读(340)  评论(0编辑  收藏  举报