using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Response.Buffer = false;
string file = "D:/bmp.bmp";
FileInfo fi = new FileInfo(file);
long startPos = 0;
//所传输的文件长度
long fileTranLen = fi.Length;
//断点续传请求
if (Request.Headers["Range"] != null)
{
Response.StatusCode = 206;
startPos = long.Parse(Request.Headers["Range"].Replace("bytes=", "").Split('-')[0]);
fileTranLen -= startPos;
//Response.AddHeader("Accept-Ranges", "bytes");
//Content-Range: bytes [文件块的开始字节]-[传输文件的总大小]/[文件的总大小]
Response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", startPos, fileTranLen, fi.Length));
}
Response.AddHeader("Content-Length", fileTranLen.ToString());
//基本的文件下载报文头
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fi.Name);
//简单的流拷贝
System.IO.Stream fileStream = System.IO.File.OpenRead(file);
fileStream.Position = startPos;
Response.End();
}
class PartDownload
{
private int offset;
private int count;
private int filesize;
private string filename;
HttpRequest Request;
HttpResponse Response;
public PartDownload(string filename,int filesize)
{
this.filename = filename;
this.filesize = filesize;
this.count = filesize;
this.offset = 0;
Request=HttpContext.Current.Request;
Response=HttpContext.Current.Response;
Response.Buffer = false;
if (Request.Headers["Range"] != null)
{
Response.StatusCode = 206;
offset = int.Parse(Request.Headers["Range"].Replace("bytes=", "").Split('-')[0]);
count = filesize - offset;
//Response.AddHeader("Accept-Ranges", "bytes");
//Content-Range: bytes [文件块的开始字节]-[传输文件的总大小]/[文件的总大小]
Response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", offset, count, filesize));
}
Response.AddHeader("Content-Length",filesize.ToString());
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
}
public void BinaryWrite(byte[] buf)
{
}
}
}
|