实例介绍
【实例截图】
【核心代码】
客户端代码:
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | /* * 作者:chxp * * 本类用于客户端文件上传、获取服务器端文件大小等 * */ using System; using System.Collections.Generic; using System.Text; using System.Net; using System.IO; using System.Collections; namespace UpLoadClient { public class UpLoadLib { /// <summary> /// 服务器端地址,部署时为Web端的地址,可以用配置文件实现 /// </summary> public static string serverPath; /// <summary> /// 上传文件(按照文件名称上传,支持断点) /// </summary> /// <param name="fileName">待上传文件全路径</param> /// <param name="byteCount">上传时的流量控制,文件较大时,用于切割文件上传</param> /// <param name="msg">错误信息</param> /// <returns>成功或者失败</returns> public static bool UpLoadFile( string fileName, int byteCount, out string msg) { msg = "" ; bool result = true ; long cruuent = GetServerFileLength(fileName); FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader bReader = new BinaryReader(fStream); long length = fStream.Length; fileName = fileName.Substring(fileName.LastIndexOf( '\\' ) 1); #region 开始上传文件 try { #region 续传处理 byte [] data; if (cruuent > 0) { fStream.Seek(cruuent, SeekOrigin.Current); } #endregion #region 分割文件上传 for (; cruuent <= length; cruuent = cruuent byteCount) { if (cruuent byteCount > length) { data = new byte [Convert.ToInt64((length - cruuent))]; bReader.Read(data, 0, Convert.ToInt32((length - cruuent))); } else { data = new byte [byteCount]; bReader.Read(data, 0, byteCount); } try { Hashtable parms = new Hashtable(); parms.Add( "fileName" , fileName); parms.Add( "npos" , cruuent.ToString()); byte [] byRemoteInfo = PostData(serverPath "UpLoadSer.aspx" , data, parms); } catch (Exception ex) { msg = ex.ToString(); result = false ; break ; } #endregion } } catch (Exception ex) { throw ex; } finally { bReader.Close(); fStream.Close(); } GC.Collect(); #endregion return result; } /// <summary> /// 调用服务器端方法,或者服务器端,同名称的文件,已经上传的大小 /// </summary> /// <param name="fileName">待上传文件全路径</param> /// <returns>服务器端文件的大小,如果以前没有上传过,则返回0</returns> private static long GetServerFileLength( string fileName) { fileName = fileName.Substring(fileName.LastIndexOf( '\\' ) 1); Hashtable parms = new Hashtable(); parms.Add( "fileName" , fileName); long length = 0; byte [] data = new byte [0]; byte [] byRemoteInfo = PostData(serverPath "GetFileLength.aspx" , data, parms); string sRemoteInfo = System.Text.Encoding.Default.GetString(byRemoteInfo); length = Convert.ToInt64(sRemoteInfo); return length; } /// <summary> /// 向服务器端提交数据 /// </summary> /// <param name="serverURL">服务器端地址</param> /// <param name="data">要发送的数据</param> /// <param name="parms">附加参数,用URL的方式将此参数附件到地址中</param> /// <returns>服务器端返回的 byte[]</returns> private static byte [] PostData( string serverURL, byte [] data, Hashtable parms) { System.Net.WebClient webClientObj = new System.Net.WebClient(); if (parms != null ) { serverURL = serverURL "?" ; foreach ( string key in parms.Keys) { serverURL = serverURL key "=" parms[key].ToString() "&" ; } serverURL.TrimEnd( '&' ); } byte [] byRemoteInfo = webClientObj.UploadData(serverURL, "POST" , data); return byRemoteInfo; } #region 测试断点上传 /// <summary> /// 测试上传100个字节(仅仅只有用于模拟断点,在真实环境中无用处) /// </summary> /// <param name="fileName">文件全路径</param> /// <param name="byteCount">上传时的流量控制,文件较大时,用于切割文件上传</param> /// <param name="msg">错误信息</param> /// <returns>成功或者失败</returns> public static bool UpLoadFileTest( string fileName, int byteCount, out string msg) { msg = "" ; bool result = true ; long cruuent = 0; FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader bReader = new BinaryReader(fStream); //模拟断点上传,第一次只上传 100 个字节 long length = 100; fileName = fileName.Substring(fileName.LastIndexOf( '\\' ) 1); #region 开始上传文件 try { byte [] data; #region 分割文件上传 for (; cruuent <= length; cruuent = cruuent byteCount) { if (cruuent byteCount > length) { data = new byte [Convert.ToInt64((length - cruuent))]; bReader.Read(data, 0, Convert.ToInt32((length - cruuent))); } else { data = new byte [byteCount]; bReader.Read(data, 0, byteCount); } try { Hashtable parms = new Hashtable(); parms.Add( "fileName" , fileName); parms.Add( "npos" , cruuent.ToString()); byte [] byRemoteInfo = PostData(serverPath "UpLoadSer100.aspx" , data, parms); } catch (Exception ex) { msg = ex.ToString(); result = false ; break ; } #endregion } } catch (Exception ex) { msg = ex.ToString(); result = false ; } finally { bReader.Close(); fStream.Close(); } GC.Collect(); #endregion return result; } #endregion } } |
服务端代码:
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 | /* * 作者:chxp * * 断点传输时的服务器端接收页面,将客户端上传的文件流,根据指针信息,保存到同名的文件流中,体现了“ 续” * */ using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; public partial class UpLoadSer : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { //服务器端保存续传的文件 SaveUpLoadFile(); } /// <summary> /// 保存文件(从URL参数中获取文件名、当前指针,将文件流保存到当前指针后) /// 如果是第一次上传,则当前指针为0,代码执行与续传一样,只不过指针没有偏移 /// </summary> public void SaveUpLoadFile() { string fileName = Request.Params[ "fileName" ]; long npos = Convert.ToInt64(Request.Params[ "npos" ]); int upLoadLength = Convert.ToInt32(Request.InputStream.Length); string path = Server.MapPath( "/UpLoadServer" ); fileName = path "//UpLoad//" fileName; FileStream fStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); //偏移指针 fStream.Seek(npos, SeekOrigin.Begin); //从客户端的请求中获取文件流 BinaryReader bReader = new BinaryReader(Request.InputStream); try { byte [] data = new byte [upLoadLength]; bReader.Read(data, 0, upLoadLength); fStream.Write(data, 0, upLoadLength); } catch { //TODO 添加异常处理 } finally { //释放流 fStream.Close(); bReader.Close(); } } } |
标签: 好用的断点传输
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论