private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { string filePath = e.Argument.ToString(); TDataInfoDTO result = new TDataInfoDTO(); result.MetaDataId = Guid.NewGuid().ToString(); result.DataName = Path.GetFileName(filePath); // result.DataDescription = txtDescription.Text; byte[] total = IOHelper.GetStreamBuffer(filePath); result.DownloadTimes = 0; result.LastModifyTime = DateTime.Now; result.CreateTime = DateTime.Now; result.BaseInfoId = _id; result.UploadPeople = _uploadBy; result.FileSize = total.Length.ToString(); bool success = _client.TDataInfoAdd(result); if (success) { byte[] buffer = new byte[4096000]; //4M int percent = total.Length / buffer.Length; for (long index = 0; index < percent; index++) { buffer = total.Skip((int)index * buffer.Length).Take(buffer.Length).ToArray(); bool upSuccess = _client.TDataInfoUploadFile(buffer, result.MetaDataId, (int)(index * buffer.Length / 40960000)); if (upSuccess == false) { e.Result = false; return; } backgroundWorker1.ReportProgress((int)((index + 1) * buffer.Length * 100 / total.Length)); manualReset.WaitOne(); } int left = total.Length % buffer.Length; if (left > 0) { byte[] leftBuffer = total.Skip(percent * buffer.Length).Take(left).ToArray(); bool upSuccess = _client.TDataInfoUploadFile(leftBuffer, result.MetaDataId, total.Length / 40960000); if (upSuccess == false) { e.Result = false; return; } manualReset.WaitOne(); backgroundWorker1.ReportProgress(100); } e.Result = true; } }
private void _bgwork_DoWork(object sender, DoWorkEventArgs e) { if (OpareteType == OpareteTypeEnum.Upload) { try { object[] arg = e.Argument as object[]; byte[] total = arg[0] as byte[]; string id = arg[1].ToString(); byte[] buffer = new byte[409600]; //400kb int percent = total.Length / buffer.Length; for (long index = 0; index < percent; index++) { buffer = total.Skip((int)index * buffer.Length).Take(buffer.Length).ToArray(); bool result = _client.TDataInfoUploadFile(buffer, id, (int)(index * buffer.Length / 40960000)); if (result == false) { e.Result = false; return; } _bgwork.ReportProgress((int)((index + 1) * buffer.Length * 100 / total.Length)); } int left = total.Length % buffer.Length; if (left > 0) { byte[] leftBuffer = total.Skip(percent * buffer.Length).Take(left).ToArray(); bool result = _client.TDataInfoUploadFile(leftBuffer, id, total.Length / 40960000); if (result == false) { e.Result = false; return; } _bgwork.ReportProgress(100); } if (_saveHistory.ToLower().EndsWith(".doc") || _saveHistory.ToLower().EndsWith(".docx") || _saveHistory.ToLower().EndsWith(".ppt") || _saveHistory.ToLower().EndsWith(".pptx") || _saveHistory.ToLower().EndsWith(".xls")) { _client.TDataInDoConvert(id); } e.Result = true; } catch (Exception) { e.Result = false; } } else { try { object[] obj = e.Argument as object[]; if (ItemData == null) { e.Result = null; return; } long totalSize = long.Parse(ItemData.FileSize); long total = 0; byte[] buffer = null; if (File.Exists(obj[0].ToString())) { File.Delete(obj[0].ToString()); } bool success = true; while (success) { using (FileStream stream = new FileStream(obj[0].ToString(), FileMode.Append)) { success = _client.TDataInfoDownloadFile(out buffer, total, ItemData.MetaDataId); if (success == false) { break; } total += buffer.Length; _bgwork.ReportProgress((int)(total * 100 / totalSize)); stream.Write(buffer, 0, buffer.Length); } } e.Result = (total == totalSize); } catch (Exception) { e.Result = false; } } }