UploadDataAsync或UploadFileAsync进度到服务器

时间:2016-02-18 23:51:25

标签: c# multithreading asynchronous file-upload client-server

我的代码应该从用户计算机上传一个zip文件到服务器,并在上传时更新进度条。它成功上传了zip文件,然而,网页一直等到它完成。只有在完成后才会进行一次更新以说明其完整。它似乎在更新之前传递了所有数据字节。我会使用// Store the tween var c = createjs.Tween.get(circle); // Create your keyframes var keyframes = [], keyframe; for (keyframe = 0; keyframe < 4; keyframe += 1) { keyframes.push({ x: 200 * keyframe, y: 200 * keyframe }); } // Do your iterations on tween keyframes.reduce(function (prev, curr, i, arr) { return prev.to({ x: arr[i].x, y: arr[i].y }); }, c.to({ x: keyframes[0].x, y: keyframes[0].y })); ,但它不能与UploadFileAsync()一起使用当我通过将PostedFile发送到FileUpload.PostedFile.FileName并使用byte[]来上传字节时,它可以正常工作。< / p>

这是在线程中开始上传的代码: (这是在没有进展的情况下正确地上传到服务器)

UploadDataAsync()

我甚至尝试使用byte[] bbb = fileMiscUploadPath.FileBytes; WebClient client = new WebClient(); Thread thread = new Thread(() => { client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged); client.UploadDataCompleted += new UploadDataCompletedEventHandler(client_UploadFileCompleted); client.UploadDataAsync(new Uri(UploadLocation), bbb); }); thread.Name = "AAAUpload"; thread.Start(); 方法,它可以在我的本地计算机上运行但是当我将其发布到服务器然后尝试上传文件时它不起作用。这是带有UploadFileAsync()的版本:(这无法在服务器上全部上传,但在运行时会上升到进度条)

UploadFileAsync()

这是clent_UploadProgressChanged函数:

WebClient client = new WebClient();
Thread thread = new Thread(() =>
{
     client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged);
     client.UploadFileCompleted += new UploadFileCompletedEventHandler(client_UploadFileCompleted);
     client.UploadFileAsync(new Uri(UploadLocation), fileMiscUploadPath.PostedFile.FileName.ToString());

 });
 thread.Name = "AAAUpload";
 thread.IsBackground = true;
 thread.Start();

确实击中了该方法,然而,它只在完成时发生一次。有没有办法在使用void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e) { byteHasBeenUploaded = true; try { double bytesIn = double.Parse(e.BytesSent.ToString()); double totalBytes = double.Parse(e.TotalBytesToSend.ToString()); double percentage = bytesIn / totalBytes * 100; UploadPercent = int.Parse(Math.Truncate(percentage).ToString()); } catch (Exception err) { UploadPercent = 999; } } 时更改缓冲区?如何分解字节以便多次触发UploadDataAsync()方法?

使用UploadProgressChanged()运行UploadProgressChangedArgs时,这些值为UploadDataAsync()Values of UploadProgressChangedEventArgs e

0 个答案:

没有答案