asp.net常用的一个很好用的控制文件下载的函数 支持续传多线(控制多线请自行修改)...

可以控制文件大小 下载名称 单块数据大小 单块数据间隔 以及MIME类型 很好用

None.gif
ExpandedBlockStart.gifContractedBlock.gif    
Public   Shared   Function ResponseFile() Function ResponseFile(ByVal _Request As HttpRequest, ByVal _Response As HttpResponse, ByVal _fileName As StringByVal _fullPath As StringByVal BlockSize As LongByVal sleep As IntegerOptional ByVal MIMEType As String = "application/octet-stream"As Boolean
InBlock.gif        
Try
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif            
Dim myFile As New FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
InBlock.gif
InBlock.gif            
Dim br As New BinaryReader(myFile)
InBlock.gif
InBlock.gif
InBlock.gif            
Try
InBlock.gif
InBlock.gif                _Response.AddHeader(
"Accept-Ranges""bytes")
InBlock.gif                _Response.Buffer 
= False
InBlock.gif                
Dim fileLength As Long = myFile.Length
InBlock.gif                
Dim startBytes As Long = 0
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif                
If (Not _Request.Headers("Range"Is NothingThen
InBlock.gif                    _Response.StatusCode 
= 206
InBlock.gif                    
Dim range() As String = _Request.Headers("Range").Split("=-".ToCharArray)
InBlock.gif                    startBytes 
= Convert.ToInt64(range(1))
InBlock.gif
InBlock.gif                
End If
InBlock.gif
InBlock.gif                _Response.AddHeader(
"Content-Length", (fileLength - startBytes).ToString())
InBlock.gif                
If (startBytes <> 0Then
InBlock.gif                    _Response.AddHeader(
"Content-Range"String.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength))
InBlock.gif
InBlock.gif                
End If
InBlock.gif                _Response.AddHeader(
"Connection""Keep-Alive")
InBlock.gif                _Response.ContentType 
= MIMEType
InBlock.gif                _Response.AddHeader(
"Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8))
InBlock.gif
InBlock.gif                br.BaseStream.Seek(startBytes, SeekOrigin.Begin)
InBlock.gif                
Dim maxCount As Integer = CInt(Math.Floor((fileLength - startBytes) / BlockSize) + 1)
InBlock.gif                
For i As Integer = 0 To maxCount - 1
InBlock.gif
InBlock.gif                    
If _Response.IsClientConnected Then
InBlock.gif                        _Response.BinaryWrite(br.ReadBytes(BlockSize))
InBlock.gif                        Thread.Sleep(sleep)
InBlock.gif
InBlock.gif                    
Else
InBlock.gif                        i 
= maxCount
InBlock.gif                    
End If
InBlock.gif
InBlock.gif                
Next
InBlock.gif            
Catch ex As Exception
InBlock.gif
InBlock.gif                
Return False
InBlock.gif            
End Try
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        
Catch ex As Exception
InBlock.gif
InBlock.gif            
Return False
InBlock.gif
InBlock.gif        
End Try
ExpandedBlockEnd.gif    
End Function

None.gif
None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值