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

首頁 > 編程 > C# > 正文

C#中使用HttpDownLoadHelper下載文件實例

2020-01-24 02:19:24
字體:
來源:轉載
供稿:網友

本文實例講述了C#使用HttpDownLoadHelper下載文件的方法。分享給大家供大家參考。具體實現方法如下:

復制代碼 代碼如下:
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);

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 都兰县| 赣榆县| 丹凤县| 万全县| 吉首市| 罗平县| 咸阳市| 沁水县| 平山县| 岐山县| 梁山县| 长垣县| 景德镇市| 上高县| 康保县| 临西县| 荔波县| 清水县| 宜城市| 辽阳县| 天全县| 沈阳市| 嘉禾县| 博客| 获嘉县| 常宁市| 洛南县| 土默特左旗| 南靖县| 潜江市| 夏津县| 汕头市| 阳春市| 寿宁县| 德州市| 无棣县| 新竹县| 崇州市| 嵊泗县| 隆昌县| 弥渡县|