欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中使用HttpDownLoadHelper下载文件实例

程序员文章站 2023-12-17 23:41:34
本文实例讲述了c#使用httpdownloadhelper下载文件的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下:using system; us...

本文实例讲述了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#程序设计有所帮助。

上一篇:

下一篇: