可以控制文件大小 下载名称 单块数据大小 单块数据间隔 以及MIME类型 很好用
Public Shared Function ResponseFile(ByVal _Request As HttpRequest, ByVal _Response As HttpResponse, ByVal _fileName As String, ByVal _fullPath As String, ByVal BlockSize As Long, ByVal sleep As Integer, Optional ByVal MIMEType As String = "application/octet-stream") As Boolean
Try
Dim myFile As New FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim br As New BinaryReader(myFile)
Try
_Response.AddHeader("Accept-Ranges", "bytes")
_Response.Buffer = False
Dim fileLength As Long = myFile.Length
Dim startBytes As Long = 0
If (Not _Request.Headers("Range") Is Nothing) Then
_Response.StatusCode = 206
Dim range() As String = _Request.Headers("Range").Split("=-".ToCharArray)
startBytes = Convert.ToInt64(range(1))
End If
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString())
If (startBytes <> 0) Then
_Response.AddHeader("Content-Range", String.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength))
End If
_Response.AddHeader("Connection", "Keep-Alive")
_Response.ContentType = MIMEType
_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8))
br.BaseStream.Seek(startBytes, SeekOrigin.Begin)
Dim maxCount As Integer = CInt(Math.Floor((fileLength - startBytes) / BlockSize) + 1)
For i As Integer = 0 To maxCount - 1
If _Response.IsClientConnected Then
_Response.BinaryWrite(br.ReadBytes(BlockSize))
Thread.Sleep(sleep)
Else
i = maxCount
End If
Next
Catch ex As Exception
Return False
End Try
Catch ex As Exception
Return False
End Try
End Function