C#中使用HttpDownLoadHelper下載文件實(shí)例
本文實(shí)例講述了C#使用HttpDownLoadHelper下載文件的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Threading;
namespace ProjectWenDangManage.Framework
{
/// <summary>
/// HTTP文件下載輔助類
/// </summary>
public class HttpDownLoadHelper
{
/// <summary>
/// 文件下載
/// </summary>
/// <param name="_Request"></param>
/// <param name="_Response"></param>
/// <param name="_fileName">下載文件時(shí)的短文件名稱</param>
/// <param name="_fullPath">待下載文件的絕對(duì)路徑</param>
/// <param name="_speed">下載速度</param>
/// <returns></returns>
public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
{
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
double pack = 10240; //10K bytes
//int sleep = 200; //每秒5次 即5*10K bytes每秒
int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
//Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor((fileLength - startBytes) / pack) + 1;
for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
return true;
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
}
}
}
HttpDownLoadHelper
在webfrom中:
HttpDownLoadHelper.ResponseFile(Request, Response, "下載顯示的名稱", filePath, 102400);
在mvc中:
HttpDownLoadHelper.ResponseFile(HttpContext.ApplicationInstance.Context.Request, HttpContext.ApplicationInstance.Context.Response, "下載顯示的名稱", filePath, 102400);
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C# 修改文件的創(chuàng)建、修改和訪問(wèn)時(shí)間的示例
這篇文章主要介紹了C#實(shí)現(xiàn)修改文件的創(chuàng)建、修改和訪問(wèn)時(shí)間的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-04-04C#使用GUID(全局統(tǒng)一標(biāo)識(shí)符)
這篇文章介紹了C#使用GUID(全局統(tǒng)一標(biāo)識(shí)符)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04.NET中的async和await關(guān)鍵字使用及Task異步調(diào)用實(shí)例
這篇文章主要介紹了.NET中的async和await關(guān)鍵字使用及Task異步調(diào)用實(shí)例,本文還包含了取消執(zhí)行和顯示進(jìn)度的例子,需要的朋友可以參考下2014-07-07C#開(kāi)發(fā)中經(jīng)常用的加密解密方法示例
這篇文章主要給大家介紹了關(guān)于C#開(kāi)發(fā)中經(jīng)常用的加密解密方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07使用C#實(shí)現(xiàn)將CSV文件內(nèi)容裝配成對(duì)象列表
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)將CSV文件內(nèi)容裝配成對(duì)象列表,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12