/// <summary> /// 分片方式上传文件流 /// </summary> #region 文件流 public void uploadStream() { // 使用uploadHost -- REMINDME // 是否使用CDN(默认:是) string uploadHost = Config.UploadFromCDN ? Config.ZONE.UploadHost : Config.ZONE.UpHost; try { this.lastModifyTime = DateTime.Now.ToFileTime(); this.size = this.fileStream.Length; //long blockCount = (this.size % Config.BLOCK_SIZE == 0) ? (this.size / Config.BLOCK_SIZE) : (this.size / Config.BLOCK_SIZE + 1); long blockCount = (this.size - 1) / Config.BLOCK_SIZE + 1; this.contexts = new string[blockCount]; } catch (Exception ex) { this.upCompletionHandler(this.key, ResponseInfo.fileError(ex), ""); return; } long offset = recoveryFromResumeRecord(); this.fileStream.Seek(offset, SeekOrigin.Begin); this.nextTask(offset, 0, uploadHost); }
/// <summary> /// 分片方式上传文件 /// </summary> #region 文件 public void uploadFile() { // 正常上传,使用UPLOAD_HOST string uploadHost = Config.UploadFromCDN ? Config.ZONE.UploadHost : Config.ZONE.UpHost; try { this.fileStream = File.Open(this.filePath, FileMode.Open, FileAccess.Read); this.lastModifyTime = File.GetLastWriteTime(this.filePath).ToFileTime(); this.size = this.fileStream.Length; //long blockCount = (this.size % Config.BLOCK_SIZE == 0) ? (this.size / Config.BLOCK_SIZE) : (this.size / Config.BLOCK_SIZE + 1); long blockCount = (this.size - 1) / Config.BLOCK_SIZE + 1; this.contexts = new string[blockCount]; } catch (Exception ex) { this.upCompletionHandler(this.key, ResponseInfo.fileError(ex), ""); return; } long offset = recoveryFromResumeRecord(); this.fileStream.Seek(offset, SeekOrigin.Begin); this.nextTask(offset, 0, uploadHost); }
/// <summary> /// 上传沙盒文件 /// </summary> /// <param name="filePath">沙盒文件全路径</param> /// <param name="key">保存在七牛的文件名</param> /// <param name="token">上传凭证</param> /// <param name="uploadOptions">上传可选设置</param> /// <param name="upCompletionHandler">上传结果处理器</param> #region 文件 public void uploadFile(string filePath, string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler) { try { long fileSize = 0; FileInfo s = new FileInfo(filePath); fileSize = s.Length; //判断文件大小,选择上传方式 if (fileSize <= Config.PUT_THRESHOLD) { new FormUploader().uploadFile(filePath, key, token, uploadOptions, upCompletionHandler); } else { string recorderKey = null; if (this.keyGenerator != null) { recorderKey = this.keyGenerator(); } new ResumeUploader(this.resumeRecorder, recorderKey, filePath, key, token, uploadOptions, upCompletionHandler).uploadFile(); } } catch (Exception ex) { if (upCompletionHandler != null) { upCompletionHandler(key, ResponseInfo.fileError(ex), null); } } }
/// <summary> /// 上传沙盒文件 /// </summary> /// <param name="filePath">沙盒文件全路径</param> /// <param name="key">保存在七牛的文件名</param> /// <param name="token">上传凭证</param> /// <param name="uploadOptions">上传可选设置</param> /// <param name="upCompletionHandler">上传结果处理器</param> #region 沙盒文件 public void uploadFile(string filePath, string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler) { try { long fileSize = 0; using (IsolatedStorageFileStream s = new IsolatedStorageFileStream(filePath, FileMode.Open, IsolatedStorageFile.GetUserStoreForApplication())) { fileSize = s.Length; } //判断文件大小,选择上传方式 if (fileSize <= Config.PUT_THRESHOLD) { new FormUploader().uploadFile(this.httpManager, filePath, key, token, uploadOptions, upCompletionHandler); } else { string recorderKey = null; if (this.keyGenerator != null) { recorderKey = this.keyGenerator(); } new ResumeUploader(this.httpManager, this.resumeRecorder, recorderKey, filePath, key, token, uploadOptions, upCompletionHandler).uploadFile(); } } catch (Exception ex) { if (upCompletionHandler != null) { upCompletionHandler(key, ResponseInfo.fileError(ex), null); } } }
/// <summary> /// 上传沙盒文件 /// </summary> /// <param name="filePath">沙盒文件全路径</param> /// <param name="key">保存在七牛的文件名</param> /// <param name="token">上传凭证</param> /// <param name="uploadOptions">上传可选设置</param> /// <param name="upCompletionHandler">上传结果处理器</param> #region 文件 public void uploadFile(string filePath, string key, string token, UploadOptions uploadOptions, UpCompletionHandler upCompletionHandler) { try { if (upCompletionHandler == null) { upCompletionHandler = DefaultUpCompletionHandler; } long fileSize = 0; FileInfo s = new FileInfo(filePath); fileSize = s.Length; //判断文件大小,选择上传方式 if (fileSize <= Config.PUT_THRESHOLD) { new FormUploader().uploadFile(filePath, key, token, uploadOptions, upCompletionHandler); } else { if (this.resumeRecorder == null) { string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); this.resumeRecorder = new ResumeRecorder(home); } string recorderKey = null; if (this.keyGenerator == null) { recorderKey = string.Format("qiniu_{0}.resume", Util.StringUtils.md5Hash(filePath + key)); } else { recorderKey = this.keyGenerator(); } new ResumeUploader(this.resumeRecorder, recorderKey, filePath, key, token, uploadOptions, upCompletionHandler).uploadFile(); } } catch (Exception ex) { if (upCompletionHandler != null) { upCompletionHandler(key, ResponseInfo.fileError(ex), null); } } }
private void makeBlock(string upHost, long offset, int blockSize, int chunkSize, ProgressHandler progressHandler, CompletionHandler completionHandler) { string url = string.Format("{0}/mkblk/{1}", upHost, blockSize); try { this.fileStream.Read(this.chunkBuffer, 0, chunkSize); } catch (Exception ex) { this.upCompletionHandler(this.key, ResponseInfo.fileError(ex), ""); return; } this.crc32 = CRC32.CheckSumBytes(this.chunkBuffer, chunkSize); post(url, this.chunkBuffer, chunkSize, progressHandler, completionHandler); }
private void putChunk(string upHost, long offset, int chunkSize, string context, ProgressHandler progressHandler, CompletionHandler completionHandler) { int chunkOffset = (int)(offset % Config.BLOCK_SIZE); string url = string.Format("{0}/bput/{1}/{2}", upHost, context, chunkOffset); try { this.fileStream.Read(this.chunkBuffer, 0, chunkSize); } catch (Exception ex) { this.upCompletionHandler(this.key, ResponseInfo.fileError(ex), ""); return; } this.crc32 = CRC32.CheckSumBytes(this.chunkBuffer, chunkSize); post(url, this.chunkBuffer, chunkSize, progressHandler, completionHandler); }
/// <summary> /// 分片方式上传文件流 /// </summary> #region 文件流 public void uploadStream() { try { this.lastModifyTime = DateTime.Now.ToFileTime(); this.size = this.fileStream.Length; long blockCount = (this.size % Config.BLOCK_SIZE == 0) ? (this.size / Config.BLOCK_SIZE) : (this.size / Config.BLOCK_SIZE + 1); this.contexts = new string[blockCount]; } catch (Exception ex) { this.upCompletionHandler(this.key, ResponseInfo.fileError(ex), ""); return; } long offset = recoveryFromResumeRecord(); this.fileStream.Seek(offset, SeekOrigin.Begin); this.nextTask(offset, 0, Config.UPLOAD_HOST); }
/// <summary> /// 分片方式上传文件 /// </summary> #region 文件 public void uploadFile() { try { this.fileStream = this.storage.OpenFile(this.filePath, FileMode.Open, FileAccess.Read); this.lastModifyTime = this.storage.GetLastWriteTime(this.filePath).ToFileTime(); this.size = this.fileStream.Length; long blockCount = (this.size % Config.BLOCK_SIZE == 0) ? (this.size / Config.BLOCK_SIZE) : (this.size / Config.BLOCK_SIZE + 1); this.contexts = new string[blockCount]; } catch (Exception ex) { this.upCompletionHandler(this.key, ResponseInfo.fileError(ex), ""); return; } long offset = recoveryFromResumeRecord(); this.nextTask(offset, 0, Config.UPLOAD_HOST); }
private void makeBlock(string upHost, long offset, int blockSize, int chunkSize, ProgressHandler progressHandler, CompletionHandler completionHandler) { string url = string.Format("{0}/mkblk/{1}", upHost, blockSize); try { this.fileStream.Read(this.chunkBuffer, 0, chunkSize); } catch (Exception ex) { this.upCompletionHandler(this.key, ResponseInfo.fileError(ex), ""); return; } this.crc32 = CRC32.CheckSumSlice(this.chunkBuffer, 0, chunkSize); this.mHttpManager.postData(url, this.upHeaders, this.chunkBuffer, 0, chunkSize, HttpManager.FORM_MIME_OCTECT, new CompletionHandler(delegate(ResponseInfo respInfo, string response) { progressHandler(offset, this.size); completionHandler(respInfo, response); })); }
private void putChunk(string upHost, long offset, int chunkSize, string context, ProgressHandler progressHandler, CompletionHandler completionHandler) { int chunkOffset = (int)(offset % Config.BLOCK_SIZE); string url = string.Format("{0}/bput/{1}/{2}", upHost, context, chunkOffset); try { this.fileStream.Read(this.chunkBuffer, 0, chunkSize); } catch (Exception ex) { this.upCompletionHandler(this.key, ResponseInfo.fileError(ex), ""); return; } this.crc32 = CRC32.CheckSumSlice(this.chunkBuffer, 0, chunkSize); this.mHttpManager.postData(url, this.upHeaders, this.chunkBuffer, 0, chunkSize, HttpManager.FORM_MIME_OCTECT, new CompletionHandler(delegate(ResponseInfo respInfo, string response) { progressHandler(offset, this.size); completionHandler(respInfo, response); })); }