/// <summary> /// 上传文件 /// </summary> /// <param name="upToken">上传Token</param> /// <param name="key">key</param> /// <param name="localFile">本地文件名</param> public async Task<CallRet> PutFile(string upToken, string localFile, string key,CancellationToken token) { if (!File.Exists(localFile)) { throw new Exception(string.Format("{0} does not exist", localFile)); } token.ThrowIfCancellationRequested(); PutAuthClient client = new PutAuthClient(upToken); CallRet ret; using (FileStream fs = File.OpenRead(localFile)) { int block_cnt = block_count(fs.Length); long fsize = fs.Length; extra.Progresses = new BlkputRet[block_cnt]; byte[] byteBuf = new byte[BLOCKSIZE]; int readLen = BLOCKSIZE; for (int i = 0; i < block_cnt; i++) { if (i == block_cnt - 1) { readLen = (int)(fsize - (long)i * BLOCKSIZE); } fs.Seek((long)i * BLOCKSIZE, SeekOrigin.Begin); fs.Read(byteBuf, 0, readLen); token.ThrowIfCancellationRequested(); BlkputRet blkRet = await ResumableBlockPut(client, byteBuf, i, readLen); if (blkRet == null) { extra.OnNotifyErr(new PutNotifyErrorEvent(i, readLen, "Make Block Error")); } else { extra.OnNotify(new PutNotifyEvent(i, readLen, extra.Progresses[i])); } } ret = await Mkfile(client, key, fsize); } if (ret.OK) { if (PutFinished != null) { PutFinished(this, ret); } } else { if (PutFailure != null) { PutFailure(this, ret); } } return ret; }
/// <summary> /// 上传文件 /// </summary> /// <param name="upToken">上传Token</param> /// <param name="key">key</param> /// <param name="localFile">本地文件名</param> public void PutFile(string upToken, string localFile, string key) { PutAuthClient client = new PutAuthClient (upToken); using (FileStream fs = File.OpenRead(localFile)) { int block_cnt = block_count (fs.Length); fsize = fs.Length; chunks = fsize / extra.chunkSize + 1; extra.Progresses = new BlkputRet[block_cnt]; //并行上传 Parallel.For (0, block_cnt, (i) => { int readLen = BLOCKSIZE; if ((i + 1) * BLOCKSIZE > fsize) readLen = (int)(fsize - i * BLOCKSIZE); byte[] byteBuf = new byte[readLen]; lock (fs) { fs.Seek (i * BLOCKSIZE, SeekOrigin.Begin); fs.Read (byteBuf, 0, readLen); BlkputRet blkRet = ResumableBlockPut (client, byteBuf, i, readLen); if (blkRet == null) { extra.OnNotifyErr (new PutNotifyErrorEvent (i, readLen, "Make Block Error")); } else { extra.OnNotify (new PutNotifyEvent (i, readLen, extra.Progresses [i])); } } }); if (string.IsNullOrEmpty (key)) { key = UNDEFINED_KEY; } CallRet ret = Mkfile (client, key, fs.Length); if (Progress != null) { Progress (1.0f); } if (PutFinished != null) { PutFinished (this, ret); } } }