国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

使用HttpDownLoadHelper下載文件

2019-11-14 16:22:48
字體:
來源:轉載
供稿:網友
using System;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">下載文件時的短文件名稱</param>        /// <param name="_fullPath">待下載文件的絕對路徑</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中

string filePath=Path.Combine(HttpRuntime.AppDomainAppPath,"Files","項目管理工具.msi");HttpDownLoadHelper.ResponseFile(Request, Response, "下載顯示的名稱", filePath, 102400);

在mvc中

string filePath=Path.Combine(HttpRuntime.AppDomainAppPath,"Files","項目管理工具.msi");HttpDownLoadHelper.ResponseFile(HttpContext.ApplicationInstance.Context.Request, HttpContext.ApplicationInstance.Context.Response, "下載顯示的名稱", filePath, 102400);

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岳普湖县| 盐源县| 白山市| 蕲春县| 东安县| 博客| 嘉峪关市| 安龙县| 黎平县| 获嘉县| 安新县| 进贤县| 前郭尔| 额尔古纳市| 英山县| 富阳市| 涡阳县| 海丰县| 蒙自县| 弥勒县| 罗田县| 怀来县| 高要市| 贵溪市| 莫力| 开封县| 酒泉市| 静安区| 双流县| 伊春市| 永兴县| 聂拉木县| 潞城市| 嘉义县| 营口市| 张北县| 诸暨市| 台中县| 西乌珠穆沁旗| 安溪县| 大姚县|