如何在步骤中显示上传进度 [英] how to show upload progress in steps

查看:76
本文介绍了如何在步骤中显示上传进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个桌面应用程序可以拍照并上传到网络服务器。我正在使用HttpWebRequest和HttpWebResponse类来发出请求并获得响应。我在UI上显示进度条以显示上传过程。一切都很好。但是由于我获得了状态,当一个文件完全上传时,我的进度条看起来已冻结,直到完整的文件上传。


Hi,

I have a desktop application which takes photos and upload to a web server. I am using HttpWebRequest and HttpWebResponse classes to make request and get response. I am showing progressbar on the UI to show upload process. Everything is working fine. But since I am getting the status, when one file gets uploaded completely, my progressbar looks frozen, till the complete file upload.

var responseFromServer = ServiceRequest.GetResponsePhoto(" AddPhotos" , " ServiceInstances" , photoToUpload, serviceInstanceID.ToString());
var response = responseFromServer.Deserialize>();
bool success = (bool )response[" success" ];







服务请求类负责发出请求并从服务器获取响应。
HttpWebResponse.GetResponseStream方法为完整上传一个文件提供响应。我想知道是否有任何方式我能够知道中间上传步骤,我可以在进度条上显示,这使得UI更具互动性。

任何帮助或任何样本将不胜感激。

Veena



ServiceRequest class takes care of making a request and getting response from server.
HttpWebResponse.GetResponseStream method gives response for the complete upload of one file. I want to know if there is any way I could able to know intermediate upload steps which I can show on the progressbar which makes UI more interactive.

Any help or any sample will be greatly appreciated.

Veena

推荐答案

是的,如果你需要中间进度指示,你应该异步上传数据。

为此,像以前一样得到流。

流stream requestStream = request.GetRequestStream();

By Byte [] photo = readPhotograph(..); //将照片放入字节数组中。或者,从流中读取。

int totalSize = photo.Length;

int offset = 0;
int totalWritten = 0;
Yeah,  if you need intermediate progress indications, you should upload the data asynchronously.

For that, get the stream as before..

Stream requestStream = request.GetRequestStream();

Byte [] photo = readPhotograph(..); // get the photo into a byte array. Or alternately, read from a stream.

int totalSize = photo.Length;

int offset = 0;
int totalWritten = 0;
void ReadCallback(IAsyncResult ar)
{
Stream stream = ar.AsyncState as Stream;
try
{
int written = stream.EndRead(ar);
offset += written;
totalWritten += written;
// here, update dialog box.
this.updateProgress(totalWritten, totalSize);

if (totalWritten < totalSize)
{
    int chunkSize = Math.Min(1024, (totalSize-totalWritten));
   // kick off another async write
   stream.BeginWrite(photo, offset, chunkSize, new AsyncCallback(ReadCallback), stream);
}
}
}



代码片段需要一些工作来分离状态变量,但你明白了......
int chunkSize = Math.Min(totalSize,1024)//以1k块写入。
requestStream.BeginWrite(photo,0,chunkSize,new AsyncCallback(WriteCallback),requestStream);
<回调中的答案...


The code snippet needs some work to separate out state variables, but you get the idea...
int chunkSize = Math.Min(totalSize, 1024) // write in 1k chunks.
requestStream.BeginWrite(photo, 0, chunkSize, new AsyncCallback(WriteCallback), requestStream);

in your callback...



这篇关于如何在步骤中显示上传进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆