C#http下载文件
特点:
(1)下载文件加上downloading后缀,下载完成再去掉后缀,
(2)含有通知下载进度事件
(3)断点续传
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | public class DownloadHelper { private int ByteSize = 1024; /// <summary> /// 下载中的后缀,下载完成去掉 /// </summary> private const string Suffix = ".downloading" ; public event Action< int > ShowDownloadPercent; /// <summary> /// Http方式下载文件 /// </summary> /// <param name="url">http地址</param> /// <param name="localfile">本地文件</param> /// <returns></returns> public int DownloadFile( string url, string localfile) { int ret = 0; string localfileReal = localfile; string localfileWithSuffix = localfileReal + Suffix; try { long startPosition = 0; FileStream writeStream = null ; if ( string .IsNullOrEmpty(url) || string .IsNullOrEmpty(localfileReal)) return 1; //取得远程文件长度 long remoteFileLength = GetHttpLength(url); if (remoteFileLength == 0) return 2; if (File.Exists(localfileReal)) return 0; //判断要下载的文件是否存在 if (File.Exists(localfileWithSuffix)) { writeStream = File.OpenWrite(localfileWithSuffix); startPosition = writeStream.Length; if (startPosition > remoteFileLength) { writeStream.Close(); File.Delete(localfileWithSuffix); writeStream = new FileStream(localfileWithSuffix, FileMode.Create); } else if (startPosition == remoteFileLength) { DownloadFileOk(localfileReal, localfileWithSuffix); writeStream.Close(); return 0; } else writeStream.Seek(startPosition, SeekOrigin.Begin); } else writeStream = new FileStream(localfileWithSuffix, FileMode.Create); HttpWebRequest req = null ; HttpWebResponse rsp = null ; try { req = (HttpWebRequest)HttpWebRequest.Create(url); if (startPosition > 0) req.AddRange(( int )startPosition); rsp = (HttpWebResponse)req.GetResponse(); using (Stream readStream = rsp.GetResponseStream()) { byte [] btArray = new byte [ByteSize]; long currPostion = startPosition; int contentSize = 0; while ((contentSize = readStream.Read(btArray, 0, btArray.Length)) > 0) { writeStream.Write(btArray, 0, contentSize); currPostion += contentSize; if (ShowDownloadPercent != null ) ShowDownloadPercent(( int )(currPostion * 100 / remoteFileLength)); } } } catch (Exception ex) { Log.Info( "获取远程文件失败!exception:\n" + ex.ToString()); ret = 3; } finally { if (writeStream != null ) writeStream.Close(); if (rsp != null ) rsp.Close(); if (req != null ) req.Abort(); if (ret == 0) DownloadFileOk(localfileReal, localfileWithSuffix); } } catch (Exception ex) { Log.Info( "获取远程文件失败!exception:\n" + ex.ToString()); ret = 4; } return ret; } /// <summary> /// 下载完成 /// </summary> private void DownloadFileOk( string localfileReal, string localfileWithSuffix) { try { //去掉.downloading后缀 FileInfo fi = new FileInfo(localfileWithSuffix); fi.MoveTo(localfileReal); } catch (Exception ex) { Log.Error(ex.ToString()); } finally { //通知完成 if (ShowDownloadPercent != null ) ShowDownloadPercent(100); } } // 从文件头得到远程文件的长度 private long GetHttpLength( string url) { long length = 0; HttpWebRequest req = null ; HttpWebResponse rsp = null ; try { req = (HttpWebRequest)HttpWebRequest.Create(url); rsp = (HttpWebResponse)req.GetResponse(); if (rsp.StatusCode == HttpStatusCode.OK) length = rsp.ContentLength; } catch (Exception ex) { Log.Info( "获取远程文件大小失败!exception:\n" + ex.ToString()); } finally { if (rsp != null ) rsp.Close(); if (req != null ) req.Abort(); } return length; } } |
参考:http://www.cnblogs.com/hayden/archive/2012/04/26/2472815.html 纯下载
参考:https://www.jb51.net/article/57068.htm 纯下载
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本