// 上传进度改变时触发 protected override void OnUploadProgressChanged(object sender, BeSl.Controls.File.Upload.UploadProgressChangedEventArgs args) { base.OnUploadProgressChanged(sender, args); progressBar.Value = TotalUploadedSize; totalSizeTextBlock.Text = string.Format("{0} / {1}", new ByteConverter().Convert(TotalUploadedSize, this.GetType(), null, null).ToString(), new ByteConverter().Convert(TotalUploadingSize, this.GetType(), null, null).ToString()); }
// 上传进度改变时触发 protected virtual void OnUploadProgressChanged(object sender, BeSl.Controls.File.Upload.UploadProgressChangedEventArgs args) { TotalUploadedSize += args.BytesUploaded; double ByteProcessTime = 0; double EstimatedTime = 0; try { TimeSpan timeSpan = DateTime.Now - m_startTime; ByteProcessTime = TotalUploadedSize / timeSpan.TotalSeconds; EstimatedTime = (TotalUploadingSize - TotalUploadedSize) / ByteProcessTime; timeSpan = TimeSpan.FromSeconds(EstimatedTime); timeLeftTextBlock.Text = string.Format("{0:00}:{1:00}:{2:00}", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds); } catch { } }
// 一次文件上传操作(由于服务器对每次请求的文件大小一般有限制,因此大文件上传时,根据ChunkSize进行分批上传) private void FileUpload_WriteCallback(IAsyncResult asynchronousResult) { HttpWebRequest webrequest = asynchronousResult.AsyncState as HttpWebRequest; // 结束操作 Stream requestStream = webrequest.EndGetRequestStream(asynchronousResult); byte[] buffer = new Byte[4096]; int bytesRead = 0; int tempTotal = 0; Stream fileStream = ((m_resizeStream != null) ? (Stream)m_resizeStream : File.OpenRead()); fileStream.Position = BytesUploaded; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0 && tempTotal + bytesRead < ChunkSize && !m_cancel) { requestStream.Write(buffer, 0, bytesRead); requestStream.Flush(); BytesUploaded += bytesRead; tempTotal += bytesRead; if (UploadProgressChanged != null) { int percent = (int)(((double)BytesUploaded / (double)FileLength) * 100); UploadProgressChangedEventArgs args = new UploadProgressChangedEventArgs(percent, bytesRead, BytesUploaded, FileLength, m_file.Name); this.Dispatcher.BeginInvoke(delegate() { UploadProgressChanged(this, args); }); } } // 只关闭文件流,不关闭图片调整流,一面第二次上传时再次对文件作调整 if (m_resizeStream == null) { fileStream.Close(); } requestStream.Close(); webrequest.BeginGetResponse(new AsyncCallback(FileUpload_ReadCallback), webrequest); //webrequest.GetResponse }