/// <summary> /// 构建分片上传对象 /// </summary> /// <param name="httpManager">HttpManager对象</param> /// <param name="recorder">分片上传进度记录器</param> /// <param name="recordKey">分片上传进度记录文件名</param> /// <param name="filePath">上传的沙盒文件全路径</param> /// <param name="key">保存在七牛的文件名</param> /// <param name="token">上传凭证</param> /// <param name="uploadOptions">上传可选设置</param> /// <param name="upCompletionHandler">上传完成结果处理器</param> public ResumeUploader(HttpManager httpManager, ResumeRecorder recorder, string recordKey, string filePath, string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler) { this.httpManager = httpManager; this.resumeRecorder = recorder; this.recordKey = recordKey; this.filePath = filePath; this.key = key; this.storage = IsolatedStorageFile.GetUserStoreForApplication(); this.uploadOptions = (uploadOptions == null) ? UploadOptions.defaultOptions() : uploadOptions; this.upCompletionHandler = new UpCompletionHandler(delegate(string fileKey, ResponseInfo respInfo, string response) { uploadOptions.ProgressHandler(key, 1.0); if (this.fileStream != null) { try { this.fileStream.Close(); } catch (Exception) { } } if (upCompletionHandler != null) { upCompletionHandler(key, respInfo, response); } }); this.httpManager.setAuthHeader("UpToken " + token); this.chunkBuffer = new byte[Config.CHUNK_SIZE]; }
public ResumeUploader(HttpManager httpManager, ResumeRecorder recorder, string recordKey, Stream stream, string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler) { this.httpManager = httpManager; this.resumeRecorder = recorder; this.recordKey = recordKey; this.fileStream = stream; this.key = key; this.storage = IsolatedStorageFile.GetUserStoreForApplication(); this.uploadOptions = (uploadOptions == null) ? UploadOptions.defaultOptions() : uploadOptions; this.upCompletionHandler = new UpCompletionHandler(delegate(string fileKey, ResponseInfo respInfo, string response) { uploadOptions.ProgressHandler(key, 1.0); if (this.fileStream != null) { try { this.fileStream.Close(); } catch (Exception) { } } if (upCompletionHandler != null) { upCompletionHandler(key, respInfo, response); } }); this.httpManager.setAuthHeader("UpToken " + token); this.chunkBuffer = new byte[Config.CHUNK_SIZE]; }
private void upload(HttpManager httpManager, PostArgs postArgs, string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler) { if (uploadOptions == null) { uploadOptions = UploadOptions.defaultOptions(); } postArgs.Params = new Dictionary<string, string>(); //设置key if (!string.IsNullOrEmpty(key)) { postArgs.Params.Add("key", key); } //设置token postArgs.Params.Add("token", token); //设置crc32校验 if (uploadOptions.CheckCrc32) { switch (httpManager.FileContentType) { case PostContentType.BYTES: postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(postArgs.Data, postArgs.Data.Length))); break; case PostContentType.STREAM: long streamLength = postArgs.Stream.Length; byte[] buffer = new byte[streamLength]; int cnt = postArgs.Stream.Read(buffer, 0, (int)streamLength); postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(buffer, cnt))); postArgs.Stream.Seek(0, SeekOrigin.Begin); break; case PostContentType.FILE: postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(postArgs.File))); break; } } //设置MimeType postArgs.MimeType = uploadOptions.MimeType; //设置扩展参数 foreach (KeyValuePair<string, string> kvp in uploadOptions.ExtraParams) { postArgs.Params.Add(kvp.Key, kvp.Value); } //设置进度处理和取消信号 httpManager.ProgressHandler = new ProgressHandler(delegate(int bytesWritten, int totalBytes) { double percent = (double)bytesWritten / totalBytes; //这样做是为了等待回复 if (percent > 0.95) { percent = 0.95; } uploadOptions.ProgressHandler(key, percent); }); httpManager.CancellationSignal = new CancellationSignal(delegate() { return uploadOptions.CancellationSignal(); }); httpManager.PostArgs = postArgs; //第一次失败后使用备用域名重试一次 httpManager.CompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response) { if (respInfo.needRetry()) { if (httpManager.PostArgs.Stream != null) { httpManager.PostArgs.Stream.Seek(0, SeekOrigin.Begin); } CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse) { uploadOptions.ProgressHandler(key, 1.0); if (httpManager.PostArgs.Stream != null) { httpManager.PostArgs.Stream.Close(); } if (upCompletionHandler != null) { upCompletionHandler(key, retryRespInfo, retryResponse); } }); httpManager.CompletionHandler = retried; httpManager.multipartPost(Config.UP_HOST); } else { uploadOptions.ProgressHandler(key, 1.0); if (httpManager.PostArgs.Stream != null) { httpManager.PostArgs.Stream.Close(); } if (upCompletionHandler != null) { upCompletionHandler(key, respInfo, response); } } }); httpManager.multipartPost(Config.UPLOAD_HOST); }
private void upload(HttpFormFile fFile, string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler) { if (uploadOptions == null) { uploadOptions = UploadOptions.defaultOptions(); } Dictionary <string, string> vPostParams = new Dictionary <string, string>(); //设置key if (!string.IsNullOrEmpty(key)) { vPostParams.Add("key", key); } //设置token vPostParams.Add("token", token); //设置crc32校验 if (uploadOptions.CheckCrc32) { switch (fFile.BodyType) { case HttpFileType.DATA_SLICE: vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(fFile.BodyBytes, fFile.Offset, fFile.Count))); break; case HttpFileType.DATA_BYTES: vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(fFile.BodyBytes))); break; case HttpFileType.FILE_STREAM: long streamLength = fFile.BodyStream.Length; byte[] buffer = new byte[streamLength]; int cnt = fFile.BodyStream.Read(buffer, 0, (int)streamLength); vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(buffer, 0, cnt))); fFile.BodyStream.Seek(0, SeekOrigin.Begin); break; case HttpFileType.FILE_PATH: vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(fFile.BodyFile))); break; } } //设置MimeType // FIX: (添加了下一行代码) // 修正上传文件MIME总为octect-stream(原因:未初始化FormFile.ContentType)的问题 // @fengyh 2016-08-17 14:50 fFile.ContentType = uploadOptions.MimeType; //设置扩展参数 foreach (KeyValuePair <string, string> kvp in uploadOptions.ExtraParams) { vPostParams.Add(kvp.Key, kvp.Value); } //设置进度处理和取消信号 ProgressHandler fUpProgressHandler = new ProgressHandler(delegate(long bytesWritten, long totalBytes) { double percent = (double)bytesWritten / totalBytes; //这样做是为了等待回复 if (percent > 0.95) { percent = 0.95; } uploadOptions.ProgressHandler(key, percent); }); CancellationSignal fCancelSignal = new CancellationSignal(delegate() { return(uploadOptions.CancellationSignal()); }); //第一次失败后使用备用域名重试一次 CompletionHandler fUpCompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response) { Console.WriteLine("form upload result, {0}", respInfo.StatusCode); if (respInfo.needRetry()) { if (fFile.BodyStream != null) { fFile.BodyStream.Seek(0, SeekOrigin.Begin); } CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse) { Console.WriteLine("form upload retry result, {0}", retryRespInfo.StatusCode); if (respInfo.isOk()) { uploadOptions.ProgressHandler(key, 1.0); } if (fFile.BodyStream != null) { fFile.BodyStream.Close(); } if (upCompletionHandler != null) { try { upCompletionHandler(key, retryRespInfo, retryResponse); } catch (Exception ex) { Console.WriteLine("form upload retry completion error, {0}", ex.Message); } } }); this.mHttpManager.postMultipartDataForm(Config.ZONE.UploadHost, null, vPostParams, fFile, fUpProgressHandler, retried); } else { if (respInfo.isOk()) { uploadOptions.ProgressHandler(key, 1.0); } if (fFile.BodyStream != null) { fFile.BodyStream.Close(); } if (upCompletionHandler != null) { try { upCompletionHandler(key, respInfo, response); } catch (Exception ex) { Console.WriteLine("form upload completion error, {0}", ex.Message); } } } }); this.mHttpManager.postMultipartDataForm(Config.ZONE.UploadHost, null, vPostParams, fFile, fUpProgressHandler, fUpCompletionHandler); }
private void upload(HttpFormFile fFile, string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler) { string uploadHost = "<UPLOAD_HOST>"; string uploadHostRetry = "<UPLOAD_HOST_RETRY>"; if(Config.UploadFromCDN) { uploadHost = Config.ZONE.UploadHost; uploadHostRetry = Config.ZONE.UpHost; } else { uploadHost = Config.ZONE.UpHost; uploadHostRetry = Config.ZONE.UploadHost; } if (uploadOptions == null) { uploadOptions = UploadOptions.defaultOptions(); } Dictionary<string, string> vPostParams = new Dictionary<string, string>(); //设置key if (!string.IsNullOrEmpty(key)) { vPostParams.Add("key", key); } //设置token vPostParams.Add("token", token); //设置crc32校验 if (uploadOptions.CheckCrc32) { switch (fFile.BodyType) { case HttpFileType.DATA_SLICE: vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(fFile.BodyBytes, fFile.Offset, fFile.Count))); break; case HttpFileType.DATA_BYTES: vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(fFile.BodyBytes))); break; case HttpFileType.FILE_STREAM: long streamLength = fFile.BodyStream.Length; byte[] buffer = new byte[streamLength]; int cnt = fFile.BodyStream.Read(buffer, 0, (int)streamLength); vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumSlice(buffer, 0, cnt))); fFile.BodyStream.Seek(0, SeekOrigin.Begin); break; case HttpFileType.FILE_PATH: vPostParams.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(fFile.BodyFile))); break; } } //设置MimeType // FIX: (添加了下一行代码) // 修正上传文件MIME总为octect-stream(原因:未初始化FormFile.ContentType)的问题 // @fengyh 2016-08-17 14:50 fFile.ContentType = uploadOptions.MimeType; //设置扩展参数 foreach (KeyValuePair<string, string> kvp in uploadOptions.ExtraParams) { vPostParams.Add(kvp.Key, kvp.Value); } //设置进度处理和取消信号 ProgressHandler fUpProgressHandler = new ProgressHandler(delegate (long bytesWritten, long totalBytes) { double percent = (double)bytesWritten / totalBytes; //这样做是为了等待回复 if (percent > 0.95) { percent = 0.95; } uploadOptions.ProgressHandler(key, percent); }); CancellationSignal fCancelSignal = new CancellationSignal(delegate () { return uploadOptions.CancellationSignal(); }); // 第一次失败后使用备用域名重试一次 CompletionHandler fUpCompletionHandler = new CompletionHandler(delegate (ResponseInfo respInfo, string response) { Console.WriteLine("form upload result, {0}",respInfo.StatusCode); if (respInfo.needRetry()) // 需要重试 { Console.WriteLine(string.Format("form upload retry")); if (Config.RetryWaitForNext) { Console.WriteLine(string.Format("wait for {0} milisecond(s)", Config.RETRY_INTERVAL_MILISEC)); System.Threading.Thread.Sleep(Config.RETRY_INTERVAL_MILISEC); } if (fFile.BodyStream != null) { fFile.BodyStream.Seek(0, SeekOrigin.Begin); } CompletionHandler retried = new CompletionHandler(delegate (ResponseInfo retryRespInfo, string retryResponse) { Console.WriteLine("form upload retry result, {0}",retryRespInfo.StatusCode); if (respInfo.isOk()) { uploadOptions.ProgressHandler(key, 1.0); } if (fFile.BodyStream != null) { fFile.BodyStream.Close(); } if (upCompletionHandler != null) { try { upCompletionHandler(key, retryRespInfo, retryResponse); } catch (Exception ex) { Console.WriteLine("form upload retry completion error, {0}", ex.Message); } } }); // 使用UPLOAD_HOST_RETRY重试 this.mHttpManager.postMultipartDataForm(uploadHostRetry, null, vPostParams, fFile, fUpProgressHandler, retried); } else // 不需要重试 { if (respInfo.isOk()) { uploadOptions.ProgressHandler(key, 1.0); } if (fFile.BodyStream != null) { fFile.BodyStream.Close(); } if (upCompletionHandler != null) { try { upCompletionHandler(key, respInfo, response); } catch (Exception ex) { Console.WriteLine("form upload completion error, {0}", ex.Message); } } } }); // 使用UPLOAD_HOST上传 this.mHttpManager.postMultipartDataForm(uploadHost, null, vPostParams, fFile, fUpProgressHandler, fUpCompletionHandler); }
private void upload(HttpManager httpManager, PostArgs postArgs, string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler) { if (uploadOptions == null) { uploadOptions = UploadOptions.defaultOptions(); } postArgs.Params = new Dictionary <string, string>(); //设置key if (!string.IsNullOrEmpty(key)) { postArgs.Params.Add("key", key); } //设置token postArgs.Params.Add("token", token); //设置crc32校验 if (uploadOptions.CheckCrc32) { switch (httpManager.FileContentType) { case PostContentType.BYTES: postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(postArgs.Data, postArgs.Data.Length))); break; case PostContentType.STREAM: long streamLength = postArgs.Stream.Length; byte[] buffer = new byte[streamLength]; int cnt = postArgs.Stream.Read(buffer, 0, (int)streamLength); postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumBytes(buffer, cnt))); postArgs.Stream.Seek(0, SeekOrigin.Begin); break; case PostContentType.FILE: postArgs.Params.Add("crc32", string.Format("{0}", CRC32.CheckSumFile(postArgs.File))); break; } } //设置MimeType postArgs.MimeType = uploadOptions.MimeType; //设置扩展参数 foreach (KeyValuePair <string, string> kvp in uploadOptions.ExtraParams) { postArgs.Params.Add(kvp.Key, kvp.Value); } //设置进度处理和取消信号 httpManager.ProgressHandler = new ProgressHandler(delegate(int bytesWritten, int totalBytes) { double percent = (double)bytesWritten / totalBytes; //这样做是为了等待回复 if (percent > 0.95) { percent = 0.95; } uploadOptions.ProgressHandler(key, percent); }); httpManager.CancellationSignal = new CancellationSignal(delegate() { return(uploadOptions.CancellationSignal()); }); httpManager.PostArgs = postArgs; //第一次失败后使用备用域名重试一次 httpManager.CompletionHandler = new CompletionHandler(delegate(ResponseInfo respInfo, string response) { if (respInfo.needRetry()) { if (httpManager.PostArgs.Stream != null) { httpManager.PostArgs.Stream.Seek(0, SeekOrigin.Begin); } CompletionHandler retried = new CompletionHandler(delegate(ResponseInfo retryRespInfo, string retryResponse) { uploadOptions.ProgressHandler(key, 1.0); if (httpManager.PostArgs.Stream != null) { httpManager.PostArgs.Stream.Close(); } if (upCompletionHandler != null) { upCompletionHandler(key, retryRespInfo, retryResponse); } }); httpManager.CompletionHandler = retried; httpManager.multipartPost(Config.UP_HOST); } else { uploadOptions.ProgressHandler(key, 1.0); if (httpManager.PostArgs.Stream != null) { httpManager.PostArgs.Stream.Close(); } if (upCompletionHandler != null) { upCompletionHandler(key, respInfo, response); } } }); httpManager.multipartPost(Config.UPLOAD_HOST); }