using System;
using System.Web;
using System.IO;
using System.Threading;
namespace Soft1000.Libs.DownLoad
{
/// <summary>
/// DownLoad 的摘要说明。
/// </summary>
public class DownLoad
{
public DownLoad()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
Public Function ResponseFile(ByVal _Request As HttpRequest, ByVal _Response 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
_Response.AddHeader("Accept-Ranges", "bytes")
_Response.Buffer = False
Dim fileLength As Long = myFile.Length
Dim startBytes As Long = 0
Dim pack As Int16 = 10240
Dim sleep As Int16 = Convert.ToInt16(Math.Floor(1000 * pack / _speed) + 1)
If Not _Request.Headers("Range") Is Nothing Then
_Response.StatusCode = 206
Dim splitChar(1) As Char
splitChar(0) = "="
splitChar(1) = "-"
Dim range() As String = _Request.Headers("Range").Split("a")
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
br.BaseStream.Seek(startBytes, SeekOrigin.Begin)
Dim maxCount As Long = Convert.ToInt64(Math.Floor((fileLength - startBytes) / pack) + 1)
Dim i As Long
For i = 0 To maxCount
If (_Response.IsClientConnected) Then
_Response.BinaryWrite(br.ReadBytes(pack))
Thread.Sleep(sleep)
Else
i = maxCount
End If
Next
Catch e As Exception
Return False
Finally
br.Close()
myFile.Close()
End Try