欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net 文件下載實(shí)現(xiàn)代碼

 更新時(shí)間:2010年03月12日 20:46:37   作者:  
文件下載代碼,Copy 別人的.需要的朋友可以參考下。
復(fù)制代碼 代碼如下:

public static bool DownloadFile(HttpContext httpContext, string filePath, long speed)
{
bool ret = true;
try
{
#region--驗(yàn)證:HttpMethod,請(qǐng)求的文件是否存在
switch (httpContext.Request.HttpMethod.ToUpper())
{ //目前只支持GET和HEAD方法
case "GET":
case "HEAD":
break;
default:
httpContext.Response.StatusCode = 501;
return false;
}
if (!File.Exists(filePath))
{
httpContext.Response.StatusCode = 404;
return false;
}
#endregion
#region 定義局部變量
long startBytes = 0;
int packSize = 1024 * 10; //分塊讀取,每塊10K bytes
string fileName = Path.GetFileName(filePath);
FileStream myFile = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
long fileLength = myFile.Length;
int sleep = (int)Math.Ceiling(1000.0 * packSize / speed);//毫秒數(shù):讀取下一數(shù)據(jù)塊的時(shí)間間隔
string lastUpdateTiemStr = File.GetLastWriteTimeUtc(filePath).ToString("r");
string eTag = HttpUtility.UrlEncode(fileName, Encoding.UTF8) + lastUpdateTiemStr;//便于恢復(fù)下載時(shí)提取請(qǐng)求頭;
#endregion
#region--驗(yàn)證:文件是否太大,是否是續(xù)傳,且在上次被請(qǐng)求的日期之后是否被修
if (myFile.Length > Int32.MaxValue)
{//-------文件太大了-------
httpContext.Response.StatusCode = 413;//請(qǐng)求實(shí)體太大
return false;
}

if (httpContext.Request.Headers["If-Range"] != null)//對(duì)應(yīng)響應(yīng)頭ETag:文件名+文件最后修改時(shí)間
{
//----------上次被請(qǐng)求的日期之后被修改過(guò)--------------
if (httpContext.Request.Headers["If-Range"].Replace("\"", "") != eTag)
{//文件修改過(guò)
httpContext.Response.StatusCode = 412;//預(yù)處理失敗
return false;
}
}
#endregion
try
{
#region -------添加重要響應(yīng)頭、解析請(qǐng)求頭、相關(guān)驗(yàn)證-------------------
httpContext.Response.Clear();
httpContext.Response.Buffer = false;
httpContext.Response.AddHeader("Content-MD5", GetMD5Hash(myFile));//用于驗(yàn)證文件
httpContext.Response.AddHeader("Accept-Ranges", "bytes");//重要:續(xù)傳必須
httpContext.Response.AppendHeader("ETag", "\"" + eTag + "\"");//重要:續(xù)傳必須
httpContext.Response.AppendHeader("Last-Modified", lastUpdateTiemStr);//把最后修改日期寫入響應(yīng)
httpContext.Response.ContentType = "application/octet-stream";//MIME類型:匹配任意文件類型
httpContext.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).Replace("+", "%20"));
httpContext.Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
httpContext.Response.AddHeader("Connection", "Keep-Alive");
httpContext.Response.ContentEncoding = Encoding.UTF8;
if (httpContext.Request.Headers["Range"] != null)
{//------如果是續(xù)傳請(qǐng)求,則獲取續(xù)傳的起始位置,即已經(jīng)下載到客戶端的字節(jié)數(shù)------
httpContext.Response.StatusCode = 206;//重要:續(xù)傳必須,表示局部范圍響應(yīng)。初始下載時(shí)默認(rèn)為200
string[] range = httpContext.Request.Headers["Range"].Split(new char[] { '=', '-' });//"bytes=1474560-"
startBytes = Convert.ToInt64(range[1]);//已經(jīng)下載的字節(jié)數(shù),即本次下載的開始位置
if (startBytes < 0 || startBytes >= fileLength)
{//無(wú)效的起始位置
return false;
}
}
if (startBytes > 0)
{//------如果是續(xù)傳請(qǐng)求,告訴客戶端本次的開始字節(jié)數(shù),總長(zhǎng)度,以便客戶端將續(xù)傳數(shù)據(jù)追加到startBytes位置后----------
httpContext.Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}
#endregion
#region -------向客戶端發(fā)送數(shù)據(jù)塊-------------------
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Ceiling((fileLength - startBytes + 0.0) / packSize);//分塊下載,剩余部分可分成的塊數(shù)
for (int i = 0; i < maxCount && httpContext.Response.IsClientConnected; i++)
{//客戶端中斷連接,則暫停
httpContext.Response.BinaryWrite(br.ReadBytes(packSize));
httpContext.Response.Flush();
if (sleep > 1) Thread.Sleep(sleep);
}
#endregion
}
catch
{
ret = false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
ret = false;
}
return ret;
}

相關(guān)文章

最新評(píng)論