.NET Framework Bookmark and Share   
 index > Network Class Library (System.Net) > how to show upload progress in steps
 

how to show upload progress in steps

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" ];



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
vsaratkar
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);
}
}
}

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...



feroze
--
My blog
Feroze Daud

You don't evenNEED to do the upload asynchronously (although that's generally a much better pattern anyways, as it allows your UI to continue to be responsive to user input during the upload). As long as you set either HttpWebRequest.ContentLength or HttpWebRequest.SendChunked before calling GetRequestStream, the data you sendwill besent to the server with each call to Stream.[Begin]Write. If you write the file in small chunks as Feroze suggests, you can get an idea of how far along you.

As a slight caution on this approach, the writes complete when the data has been sent, NOT when it has actually been received by the server. On a high-latency connection, the lastchunk of data may be sentwell before the server actually receives it. It may be worth reserving some space at the end of your progress bar for the call to [Begin]GetResponse, after which you can actually be sure that the server has received the entire file.

As another caution, writing lots of small chunks can be less efficient than writing a few large chunks. If raw upload speed is important to your application, it may be worth writing the file in large chunks and not being as precise with the progress bar.

-dave

Dave Murray [NCL]
That is a good point, Dave. I had'nt thought about the fact that this would only track completions on the client, not the actual reception of data on the server.

Also, you made some good points about chunk sizes.

feroze
--
My blog
Feroze Daud
Thanks Feroze and Dave,

I will try HttpWebRequest.ContentLength or HttpWebRequest.SendChunked.


Veena
vsaratkar

You can use google to search for other answers

Custom Search

More Threads

• change Internet Explorer Proxy setting
• Http post to a WML page
• Problem sending an email
• Sockets
• downloading all files in directory http and c#
• FTP on IIS return 421 timeout to my FtpWebRequest
• vb 2008 System.Net.Sockets.TcpClient Connection State
• HttpWebRequest Sending Multiple Packets
• NetworkStream.BeginWrite - very slow!?!
• NegotiateStream delegation problem