Note that there are some explanatory texts on larger screens.

plurals
  1. POFile upload progress
    text
    copied!<p>I've been trying to track the progress of a file upload but keep on ending up at dead ends (uploading from a C# application not a webpage).</p> <p>I tried using the <code>WebClient</code> as such:</p> <pre><code>class Program { static volatile bool busy = true; static void Main(string[] args) { WebClient client = new WebClient(); // Add some custom header information client.Credentials = new NetworkCredential("username", "password"); client.UploadProgressChanged += client_UploadProgressChanged; client.UploadFileCompleted += client_UploadFileCompleted; client.UploadFileAsync(new Uri("http://uploaduri/"), "filename"); while (busy) { Thread.Sleep(100); } Console.WriteLine("Done: press enter to exit"); Console.ReadLine(); } static void client_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e) { busy = false; } static void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e) { Console.WriteLine("Completed {0} of {1} bytes", e.BytesSent, e.TotalBytesToSend); } } </code></pre> <p>The file does upload and progress is printed out but the progress is much faster than the actual upload and when uploading a large file the progress will reach the maximum within a few seconds but the actual upload takes a few minutes (it is not just waiting on a response, all the data have not yet arrived at the server).</p> <p>So I tried using <code>HttpWebRequest</code> to stream the data instead (I know this is not the exact equivalent of a file upload as it does not produce <code>multipart/form-data</code> content but it does serve to illustrate my problem). I set <code>AllowWriteStreamBuffering = false</code> and set the <code>ContentLength</code> as suggested by <a href="https://stackoverflow.com/questions/2208299/c-httpwebrequest-form-post-with-progress-tracking-for-uploading-potentially-lar">this question/answer</a>:</p> <pre><code>class Program { static void Main(string[] args) { FileInfo fileInfo = new FileInfo(args[0]); HttpWebRequest client = (HttpWebRequest)WebRequest.Create(new Uri("http://uploadUri/")); // Add some custom header info client.Credentials = new NetworkCredential("username", "password"); client.AllowWriteStreamBuffering = false; client.ContentLength = fileInfo.Length; client.Method = "POST"; long fileSize = fileInfo.Length; using (FileStream stream = fileInfo.OpenRead()) { using (Stream uploadStream = client.GetRequestStream()) { long totalWritten = 0; byte[] buffer = new byte[3000]; int bytesRead = 0; while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) &gt; 0) { uploadStream.Write(buffer, 0, bytesRead); uploadStream.Flush(); Console.WriteLine("{0} of {1} written", totalWritten += bytesRead, fileSize); } } } Console.WriteLine("Done: press enter to exit"); Console.ReadLine(); } } </code></pre> <p>The request does not start until the entire file have been written to the stream and already shows full progress at the time it starts (I'm using <a href="http://www.fiddler2.com/fiddler2/" rel="nofollow noreferrer">fiddler</a> to verify this). I also tried setting <code>SendChunked</code> to true (with and without setting the <code>ContentLength</code> as well). It seems like the data still gets cached before being sent over the network.</p> <p>Is there something wrong with one of these approaches or is there perhaps another way I can track the progress of file uploads from a windows application?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload