using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;
public partial class FilUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.Response.Clear();
bool success = ResponseFile("零点过半.rar", @"C:\download.rar", 1024000);
if (!success)
{
Response.Write("下载文件出错!");
}
Page.Response.End();
}
bool ResponseFile(string fileName, string fullPath, long speed) // 输入参数 fileName: 下载文件名, fullPath: 带文件名下载路径, speed 每秒允许下载的字节数
{
try
{
FileStream myFile = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read); //以只读多进程共享形式打开硬盘文件
BinaryReader br = new BinaryReader(myFile); //以二进制数据流形式读取文件
try
{
Page.Response.AddHeader("Accept-Ranges", "bytes");
Page.Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
int pack = 10240; //10K bytes
//int sleep = 200; //每秒5次 即5*10K bytes每秒
int sleep = (int)Math.Floor((double)(1000 * pack / speed)) + 1;
if (Page.Request.Headers["Range"] != null)
{
Page.Response.StatusCode = 206;
string[] range = Page.Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
}
Page.Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
Page.Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}
Page.Response.AddHeader("Connection", "Keep-Alive");
Page.Response.ContentType = "application/octet-stream";
Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); //使用中文文件名必须使用UTF8编码
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor((double)((fileLength - startBytes) /pack)) + 1;
for (int i = 0; i < maxCount; i++)
{
if (Page.Response.IsClientConnected)
{
Page.Response.BinaryWrite(br.ReadBytes(pack));
System.Threading.Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}
}
在IE上成功,但是在360浏览器上运行不显示下载的界面