Link to home
Create AccountLog in
Avatar of Enill
Enill

asked on

WebClient || HttpWebRequest POST Upload Progress

Hi,
i have a WCF REST Service with a WebInvoke method used to upload image to the server.

I try to keep track of the progress by using a WebClient.UploadDataAsync but it doesnt work.

The UploadProgressChanged event jump to 50% then directly to 100%.(e.ProgresPercentage).

I tried with UploadFileAsync and it doesnt work either it jump to 50% then crash and i get a response from the server saying that an error was encountered so the file is never sent to the server.

Side Note: With UploadDataSync the file do reach the server perfectly.

Can anyone help me with this? Or do you have an exemple using HttpWebRequest with a progress indicator?

Thanks in advance!
ASKER CERTIFIED SOLUTION
Avatar of Sudhakar Pulivarthi
Sudhakar Pulivarthi
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Enill
Enill

ASKER

Thanks a lot, i'll give it a try once at home.
Avatar of Enill

ASKER

Hi,
i tested your code but it does seem to work like it should unless i am misunderstanding something.

If i add a new variable 'segment' and increment it by 1 for every while loop and calculate a percentage by comparing segment and segments, it will just jump from 0 to 100% and it look like it will only start to transfer the file when WebResponse response = request.GetResponse() is called.

                // Write the source data to the network stream.
                int segment = 0;
                using (Stream requestStream = request.GetRequestStream())
                {
                    // Loop till the file content is read completely.
                    while ((bytesRead = fileStream.Read(tempBuffer, 0, tempBuffer.Length)) > 0)
                    {
                        // Write the 4 KB data in the buffer to the network stream.
                        requestStream.Write(tempBuffer, 0, bytesRead);

                        segment++;

                        //// Update your progress bar here using segment count.
                        Console.WriteLine("Progress: {0} %", ((segment * 100) / segments));
                    }
                }
            }

Open in new window

Avatar of Enill

ASKER

I added these lines but it changed nothing:

request.AllowWriteStreamBuffering = false;
request.ServicePoint.Expect100Continue = false;

Open in new window

Hi Expert,
Sorry for the late reply.
correct the progress value as ((segment * 100.0) / segments) so that you will get the accurate increase in progress. Round off the value if neccessary.
The Client will send the upload data using GetRequestStream from the server and after closing the request stream on call to response will update the data from the stream to the file in server and gives the response to the client.
Read more about the GetRequestStream() : http://msdn.microsoft.com/en-us/library/d4cek6cc(v=VS.90).aspx

Hope this will solve your issue.
Avatar of Enill

ASKER

Hi and thanks for the reply.

Actually im not sure what is wrong with the code.

For a 30mb file Upload it jump from 0 to 100% in approximatly 2sec. and then start to really upload the file.

Can you attach a working example with true progression from 0% to 100%.

Thanks!
Avatar of Enill

ASKER

 
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("my_address_for_the_post");
            request.Method = "POST";
            request.ContentType = "text/plain";
            //request.AllowWriteStreamBuffering = false;
            //request.ServicePoint.Expect100Continue = false;
            //request.SendChunked = true;
 
            request.Timeout = -1;
 
            FileInfo fileInfo = new FileInfo(@"C:\mi_file");
 
            request.ContentLength = fileInfo.Length;
 
            int segments = Convert.ToInt32(fileInfo.Length / (1024 * 4));
 
            using (FileStream fileStream = fileInfo.OpenRead())
            {
                byte[] tempBuffer = new byte[1024 * 4];
                int bytesRead = 0;
 
                int segment = 0;
                using (Stream requestStream = request.GetRequestStream())
                {
                    while ((bytesRead = fileStream.Read(tempBuffer, 0, tempBuffer.Length)) > 0)
                    {
                        requestStream.Write(tempBuffer, 0, bytesRead);
 
                        segment++;
 
                        Console.WriteLine("{0} - Progress: {1} %", segment, ((segment * 100.0) / segments));
                    }
                }
            }
 
            using (WebResponse response = request.GetResponse())
            {
               //
            }

Open in new window

Avatar of Enill

ASKER

Ok, i figured keeping Fiddler open was causing this issue.

Hell, it was not easy to figure out that this was the problem.

Thanks a lot.