前些天8月以前文件下载还能用,最近做了几次windows更新就挂了!!!!!!!!
微软你到底做了些什么??????
文件下载的代码如下:
using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Threading;
namespace FileDownLoad4Net
{
/// <summary>
/// FileDown 的摘要说明。
/// </summary>
public class FileDown
{
private string m_strFullPath;
private long m_lSpeed;
private int m_iFactDownSpeed;
private string m_strFileName;
/// <summary>
/// 下载页显示文件名
/// </summary>
public string FileName
{
get{return m_strFileName;}
set{m_strFileName = value;}
}
/// <summary>
/// 带文件名下载路径
/// </summary>
public string FullPath
{
get{return m_strFullPath;}
set{m_strFullPath = value;}
}
/// <summary>
/// 每秒允许下载的字节数
/// </summary>
public long Speed
{
get{return m_lSpeed;}
set{m_lSpeed = value;}
}
/// <summary>
/// 实际下载速度
/// </summary>
public int FactDownSpeed
{
get{return m_iFactDownSpeed;}
set{m_iFactDownSpeed = value;}
}
/// <summary>
/// 表示一个下载
/// </summary>
public FileDown()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 开始下载文件
/// </summary>
/// <returns></returns>
public bool DownFile()
{
return DownFile(this.FileName,this.FullPath,this.Speed);
}
/// <summary>
/// 开始下载文件
/// </summary>
/// <param name="p_strFileName">下载页显示文件名</param>
/// <param name="p_strFullPath">带文件名下载路径</param>
/// <param name="p_lSpeed">每秒允许下载的字节数</param>
/// <returns></returns>
public bool DownFile(string p_strFileName,string p_strFullPath, long p_lSpeed)
{
bool bRtn = false;
System.Web.UI.Page p_page = (Page)System.Web.HttpContext.Current.Handler;
//try1
try
{
#region "try1"
FileStream myFile = new FileStream(p_strFullPath, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
//try2
try
{
#region "try2"
//p_page.Response.Clear();
p_page.Response.AppendHeader("Accept-Ranges", "bytes");
p_page.Response.BufferOutput = 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(1000 * pack / p_lSpeed) + 1;
if (p_page.Request.Headers["Range"] != null)
{
p_page.Response.StatusCode = 206;
string[] range = p_page.Request.Headers["Range"].Split(new char[] {'=', '-'});
startBytes = Convert.ToInt64(range[1]);
}
p_page.Response.AppendHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
p_page.Response.AppendHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
}
p_page.Response.AppendHeader("Connection", "Keep-Alive");
p_page.Response.ContentType = "application/octet-stream";
p_page.Response.AppendHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(p_strFileName,System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;
DateTime DownTimeStart = DateTime.Now;
for (int i = 0; i < maxCount; i++)
{
if(i == 1)
{
DownTimeStart = DateTime.Now;
}
if (p_page.Response.IsClientConnected)
{
p_page.Response.BinaryWrite(br.ReadBytes(pack));
p_page.Response.Flush();
Thread.Sleep(sleep);
//开始计算时间
bRtn = true;
}
else
{
maxCount = i;
bRtn = false;
break;
}
}
#endregion
System.TimeSpan tsDownTimeLen = DateTime.Now -DownTimeStart;
this.FactDownSpeed = (int)Math.Floor((int) Math.Floor((startBytes + maxCount * pack ) / tsDownTimeLen.TotalSeconds) / 1024);
}
catch
{
bRtn = false;
}
finally
{
p_page.Response.Flush();
br.Close();
myFile.Close();
}
#endregion
}
catch
{
bRtn = false;
}
return bRtn;
}
}
}