美文网首页
C#文件下载工具类,带文件进度、文件下载完成回调

C#文件下载工具类,带文件进度、文件下载完成回调

作者: Shane_Roy | 来源:发表于2018-03-09 11:26 被阅读0次

    c#中文件下载工具类,可以实现文件进度和下载完成(成功、失败)的回调,具体使用请往下翻

    public class FileDownloadUtil
    {
        private string url; //文件下载网络地址
        private string path; //文件下载位置,如d:/download
        private string filename; //文件名,如test.jpg
        private string fileId; //文件ID,文件唯一标识,一般为UUID
    
        public delegate void ProgressChangedHandler(int progress, string fileId);
        public event ProgressChangedHandler ProgressChanged;
        protected virtual void OnProgressChanged(int progress, string fileId)
        {
            ProgressChanged?.Invoke(progress, fileId);
        }
        public delegate void DownloadFinishHandler(bool isSuccess, string downloadPath, string fileId, string msg = null);
        public event DownloadFinishHandler DownloadFinish;
        protected virtual void OnDownloadFinish(bool isSuccess, string downloadPath, string fileId, string msg = null)
        {
            DownloadFinish?.Invoke(isSuccess, downloadPath, fileId, msg);
        }
    
        //通过网络链接直接下载任意文件
        public FileDownloadUtil(string url, string path, string filename, string fileId)
        {
            this.url = url;
            this.path = path;
            this.filename = filename;
            this.fileId = fileId;
        }
    
        public void Download()
        {
            Download(url, path, filename, fileId);
        }
        private void Download(string url, string path, string filename, string fileId)
        {
    
            if (!Directory.Exists(path))   //判断文件夹是否存在
                Directory.CreateDirectory(path);
            path = path + "/" + filename;
            string tempFile = path + ".temp"; //临时文件
            if (File.Exists(tempFile))
            {
                File.Delete(tempFile);    //存在则删除
            }
            if (File.Exists(path))
            {
                File.Delete(path);    //存在则删除
            }
            FileStream fs = null;
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            Stream responseStream = null;
            try
            {
                //创建临时文件
                fs = new FileStream(tempFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                request = WebRequest.Create(url) as HttpWebRequest;
                //发送请求并获取相应回应数据
                response = request.GetResponse() as HttpWebResponse;
                //直到request.GetResponse()程序才开始向目标网页发送Post请求
                responseStream = response.GetResponseStream();
                byte[] bArr = new byte[1024];
                long totalBytes = response.ContentLength; //通过响应头获取文件大小,前提是响应头有文件大小
                int size = responseStream.Read(bArr, 0, (int)bArr.Length); //读取响应流到bArr,读取大小
                float percent = 0;  //用来保存计算好的百分比
                long totalDownloadedByte = 0;  //总共下载字节数
                while (size > 0)  //while循环读取响应流
                {
                    fs.Write(bArr, 0, size); //写到临时文件
                    totalDownloadedByte += size; 
                    size = responseStream.Read(bArr, 0, (int)bArr.Length);
                    percent = (float)totalDownloadedByte / (float)totalBytes * 100;
                    OnProgressChanged((int)percent, fileId);  //下载进度回调
                }
                if (File.Exists(path))
                {
                    File.Delete(path);    //存在则删除
                }
                File.Move(tempFile, path); //重命名为正式文件
                OnDownloadFinish(true, path, fileId, null);  //下载完成,成功回调
            }
            catch (Exception ex)
            {
                OnDownloadFinish(false, null, fileId, ex.Message);  //下载完成,失败回调
            }
            finally
            {
                if (fs != null)
                    fs.Close();
                if (request != null)
                    request.Abort();
                if (response != null)
                    response.Close();
                if (responseStream != null)
                    responseStream.Close();
            }
        }
    }
    

    如何使用:

    public void TestDownload()
    {
        FileDownloadUtil file = new FileDownloadUtil("需要下载的网络地址","文件下载到的文件夹路径","带后缀的文件名","文件ID,按需求自行删除");
        file.ProgressChanged += File_ProgressChanged;
        file.DownloadFinish += File_DownloadFinish;
        file.Download();
    }
    
    private void File_DownloadFinish(bool isSuccess, string downloadPath, string fileId, string msg = null)
    {
        if (isSuccess)
        {
            Console.WriteLine("文件 " + fileId + " 下载成功,保存位置:"+downloadPath);
        }
        else
        {
            Console.WriteLine("文件 " + fileId + " 下载失败 " + msg==null ? "":"原因:"+msg);
        }
    
    }
    
    private void File_ProgressChanged(int progress, string fileId)
    {
        Console.WriteLine("文件 " + fileId + " 下载进度:" + progress);
    }
    

    相关文章

      网友评论

          本文标题:C#文件下载工具类,带文件进度、文件下载完成回调

          本文链接:https://www.haomeiwen.com/subject/oyjpfftx.html