<p>Is it possible to get the progress of an XMLHttpRequest (bytes uploaded, bytes downloaded)? </p>

<p>This would be useful to show a progress bar when the user is uploading a large file. The standard API doesn't seem to support it, but maybe there's some non-standard extension in any of the browsers out there? It seems like a pretty obvious feature to have after all, since the client knows how many bytes were uploaded/downloaded.</p>

<p>note: I'm aware of the "poll the server for progress" alternative (it's what I'm doing right now). the main problem with this (other than the complicated server-side code) is that typically, while uploading a big file, the user's connection is completely hosed, because most ISPs offer poor upstream. So making extra requests is not as responsive as I'd hoped. I was hoping there'd be a way (maybe non-standard) to get this information, which the browser has at all times.</p>

[{'answer_id': 77075, 'author': 'Sean McMains', 'author_id': 2041950, 'author_profile': 'https://Stackoverflow.com/users/2041950', 'pm_score': 3, 'selected': False, 'text': '<p>One of the most promising approaches seems to be opening a second communication channel back to the server to ask it how much of the transfer has been completed.</p>\n'}, {'answer_id': 77098, 'author': 'Aeon', 'author_id': 13289, 'author_profile': 'https://Stackoverflow.com/users/13289', 'pm_score': 1, 'selected': False, 'text': '<p>If you have access to your apache install and trust third-party code, you can use the <a href="http://drogomir.com/blog/2008/6/18/upload-progress-bar-with-mod_passenger-and-apache" rel="nofollow noreferrer">apache upload progress module</a> (if you use apache; there\'s also a <a href="http://wiki.codemongers.com/NginxHttpUploadProgressModule" rel="nofollow noreferrer">nginx upload progress module</a>). </p>\n\n<p>Otherwise, you\'d have to write a script that you can hit out of band to request the status of the file (checking the filesize of the tmp file for instance). </p>\n\n<p>There\'s some work going on in firefox 3 I believe to add upload progress support to the browser, but that\'s not going to get into all the browsers and be widely adopted for a while (more\'s the pity).</p>\n'}, {'answer_id': 77163, 'author': 'Orclev', 'author_id': 13739, 'author_profile': 'https://Stackoverflow.com/users/13739', 'pm_score': 2, 'selected': False, 'text': "<p>For the total uploaded there doesn't seem to be a way to handle that, but there's something similar to what you want for download. Once readyState is 3, you can periodically query responseText to get all the content downloaded so far as a String (this doesn't work in IE), up until all of it is available at which point it will transition to readyState 4. The total bytes downloaded at any given time will be equal to the total bytes in the string stored in responseText.</p>\n\n<p>For a all or nothing approach to the upload question, since you have to pass a string for upload (and it's possible to determine the total bytes of that) the total bytes sent for readyState 0 and 1 will be 0, and the total for readyState 2 will be the total bytes in the string you passed in. The total bytes both sent and received in readyState 3 and 4 will be the sum of the bytes in the original string plus the total bytes in responseText.</p>\n"}, {'answer_id': 77284, 'author': 'Alexandre Victoor', 'author_id': 11897, 'author_profile': 'https://Stackoverflow.com/users/11897', 'pm_score': -1, 'selected': False, 'text': '<p>The only way to do that with pure javascript is to implement some kind of polling mechanism.\nYou will need to send ajax requests at fixed intervals (each 5 seconds for example) to get the number of bytes received by the server.</p>\n\n<p>A more efficient way would be to use flash. The flex component <a href="http://livedocs.adobe.com/flex/3/langref/flash/net/FileReference.html" rel="nofollow noreferrer">FileReference</a> dispatchs periodically a \'progress\' event holding the number of bytes already uploaded. \nIf you need to stick with javascript, bridges are available between actionscript and javascript. \nThe good news is that this work has been already done for you :)</p>\n\n<p><a href="http://www.swfupload.org" rel="nofollow noreferrer"><strong>swfupload</strong></a></p>\n\n<p>This library allows to register a javascript handler on the flash progress event.</p>\n\n<p>This solution has the hudge advantage of not requiring aditionnal resources on the server side.</p>\n'}, {'answer_id': 3360576, 'author': 'Markus Peröbner', 'author_id': 404522, 'author_profile': 'https://Stackoverflow.com/users/404522', 'pm_score': 3, 'selected': False, 'text': '<p>Firefox supports <a href="https://developer.mozilla.org/en/using_xmlhttprequest#Monitoring_progress" rel="nofollow noreferrer">XHR download progress events</a>.</p>\n<p><strong>EDIT 2021-07-08 10:30 PDT</strong></p>\n<p>The above link is dead.  Doing a search on the Mozilla WebDev site turned up the following link:</p>\n<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/ProgressEvent" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Web/API/ProgressEvent</a></p>\n<p>It describes how to use the progress event with XMLHttpRequest and provides an example.  I\'ve included the example below:</p>\n<pre><code>var progressBar = document.getElementById(&quot;p&quot;),\n    client = new XMLHttpRequest()\nclient.open(&quot;GET&quot;, &quot;magical-unicorns&quot;)\nclient.onprogress = function(pe) {\n  if(pe.lengthComputable) {\n    progressBar.max = pe.total\n    progressBar.value = pe.loaded\n  }\n}\nclient.onloadend = function(pe) {\n  progressBar.value = pe.loaded\n}\nclient.send()\n</code></pre>\n<p>I also found this link as well which is what I think the original link pointed to.</p>\n<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/progress_event" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/progress_event</a></p>\n'}, {'answer_id': 3694435, 'author': 'albanx', 'author_id': 354881, 'author_profile': 'https://Stackoverflow.com/users/354881', 'pm_score': 7, 'selected': False, 'text': '<p>For the bytes uploaded it is quite easy. Just monitor the <code>xhr.upload.onprogress</code> event. The browser knows the size of the files it has to upload and the size of the uploaded data, so it can provide the progress info.</p>\n\n<p>For the bytes downloaded (when getting the info with <code>xhr.responseText</code>), it is a little bit more difficult, because the browser doesn\'t know how many bytes will be sent in the server request. The only thing that the browser knows in this case is the size of the bytes it is receiving. </p>\n\n<p>There is a solution for this, it\'s sufficient to set a <code>Content-Length</code> header on the server script, in order to get the total size of the bytes the browser is going to receive. </p>\n\n<p>For more go to <a href="https://developer.mozilla.org/en/Using_XMLHttpRequest" rel="noreferrer">https://developer.mozilla.org/en/Using_XMLHttpRequest</a> .</p>\n\n<p>Example:\nMy server script reads a zip file (it takes 5 seconds):</p>\n\n<pre><code>$filesize=filesize(\'test.zip\');\n\nheader("Content-Length: " . $filesize); // set header length\n// if the headers is not set then the evt.loaded will be 0\nreadfile(\'test.zip\');\nexit 0;\n</code></pre>\n\n<p>Now I can monitor the download process of the server script, because I know it\'s total length:</p>\n\n<pre><code>function updateProgress(evt) \n{\n   if (evt.lengthComputable) \n   {  // evt.loaded the bytes the browser received\n      // evt.total the total bytes set by the header\n      // jQuery UI progress bar to show the progress on screen\n     var percentComplete = (evt.loaded / evt.total) * 100;  \n     $(\'#progressbar\').progressbar( "option", "value", percentComplete );\n   } \n}   \nfunction sendreq(evt) \n{  \n    var req = new XMLHttpRequest(); \n    $(\'#progressbar\').progressbar();    \n    req.onprogress = updateProgress;\n    req.open(\'GET\', \'test.php\', true);  \n    req.onreadystatechange = function (aEvt) {  \n        if (req.readyState == 4) \n        {  \n             //run any callback here\n        }  \n    };  \n    req.send(); \n}\n</code></pre>\n'}, {'answer_id': 35080857, 'author': 'Forums Lover', 'author_id': 5283607, 'author_profile': 'https://Stackoverflow.com/users/5283607', 'pm_score': 2, 'selected': False, 'text': '<p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;body&gt;\r\n&lt;p id="demo"&gt;result&lt;/p&gt;\r\n&lt;button type="button" onclick="get_post_ajax();"&gt;Change Content&lt;/button&gt;\r\n&lt;script type="text/javascript"&gt;\r\n function update_progress(e)\r\n {\r\n   if (e.lengthComputable)\r\n   {\r\n     var percentage = Math.round((e.loaded/e.total)*100);\r\n     console.log("percent " + percentage + \'%\' );\r\n   }\r\n   else \r\n   {\r\n    console.log("Unable to compute progress information since the total size is unknown");\r\n   }\r\n }\r\n function transfer_complete(e){console.log("The transfer is complete.");}\r\n function transfer_failed(e){console.log("An error occurred while transferring the file.");}\r\n function transfer_canceled(e){console.log("The transfer has been canceled by the user.");}\r\n function get_post_ajax()\r\n {\r\n    var xhttp;\r\n    if (window.XMLHttpRequest){xhttp = new XMLHttpRequest();}//code for modern browsers} \r\n   else{xhttp = new ActiveXObject("Microsoft.XMLHTTP");}// code for IE6, IE5    \r\n    xhttp.onprogress = update_progress;\r\n  xhttp.addEventListener("load", transfer_complete, false);\r\n  xhttp.addEventListener("error", transfer_failed, false);\r\n  xhttp.addEventListener("abort", transfer_canceled, false);    \r\n    xhttp.onreadystatechange = function()\r\n    {\r\n      if (xhttp.readyState == 4 &amp;&amp; xhttp.status == 200)\r\n      {\r\n         document.getElementById("demo").innerHTML = xhttp.responseText;\r\n      }\r\n    };\r\n   xhttp.open("GET", "http://it-tu.com/ajax_test.php", true);\r\n   xhttp.send();\r\n }\r\n&lt;/script&gt;\r\n&lt;/body&gt;\r\n&lt;/html&gt;</code></pre>\r\n</div>\r\n</div>\r\n</p>\n\n<p><a href="https://i.stack.imgur.com/K0f91.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/K0f91.jpg" alt="Result"></a></p>\n'}]
['https://Stackoverflow.com/questions/76976', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']