<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>lighty's life: mod_uploadprogress is back</title>
    <link>http://blog.lighttpd.net/articles/2006/08/01/mod_uploadprogress-is-back</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>mod_uploadprogress is back</title>
      <description>&lt;p&gt;In my ongoing efforts in restructuring the core of lighttpd for the 1.5.0 release I brought mod_uploadprogress back to life.&lt;/p&gt;
&lt;p&gt;I talked about mod_uploadprogress a long time ago and since then I got at least one request per month to bring it back again.&lt;/p&gt;


It was changed abit since the early days. Instead of &lt;span class="caps"&gt;XML&lt;/span&gt; we send out &lt;span class="caps"&gt;JSON&lt;/span&gt; now. That does the same and easier to parse:
&lt;pre&gt;
{ 'state' : 'starting' };
...
{ 'state' : 'uploading', 'size' : 87901150, 'received' : 80601372};
{ 'state' : 'done', 'size' : 87901150, 'received' : 87901150};
&lt;/pre&gt;

	&lt;p&gt;First you need a file-upload form. It is all standard and only calls our upload-progress updater onSubmit.&lt;/p&gt;


&lt;pre&gt;
  &amp;lt;form id="upload" enctype="multipart/form-data" 
    action="/upload.php" method="post" 
    onsubmit="openProgressBar(); return true;"&amp;gt;
  &amp;lt;input type="hidden" name="MAX_FILE_SIZE" value="30000000"  /&amp;gt;
  &amp;lt;input name="userfile" type="file" label="fileupload" /&amp;gt;
  &amp;lt;input type="submit" value="Send File" /&amp;gt;
  &amp;lt;/form&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;The progress bar in this case are 2 nested div-tags:&lt;/p&gt;


&lt;pre&gt;
  &amp;lt;div&amp;gt;
   &amp;lt;div id="progress" style="width: 400px; border: 1px solid black"&amp;gt;
    &amp;lt;div id="progressbar" 
       style="width: 1px; background-color: black; border: 1px solid white"&amp;gt;
     &amp;amp;nbsp;
    &amp;lt;/div&amp;gt;
   &amp;lt;/div&amp;gt;
   &amp;lt;div id="tp"&amp;gt;(throughput)&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;Some Javascript with XMLHTTPRequest can tell us about the state of a upload:&lt;/p&gt;


&lt;pre&gt;
&amp;lt;script type="text/javascript"&amp;gt;
 interval = null;

function fetch(uuid) {
 req = new XMLHttpRequest();
 req.open("GET", "/progress", 1);
 req.setRequestHeader("X-Progress-Id", uuid);
 req.onreadystatechange = function () {
  if (req.readyState == 4) {
   if (req.status == 200) {
    /* poor-man JSON parser */
    var upload = eval(req.responseText);

    document.getElementById('tp').innerHTML = upload.state;

    /* change the width if the inner progress-bar */
    if (upload.state == 'done' || upload.state == 'uploading') {
     bar = document.getElementById('progressbar');
     w = 400 * upload.received / upload.size;
     bar.style.width = w + 'px';
    }
    /* we are done, stop the interval */
    if (upload.state == 'done') {
     window.clearTimeout(interval);
    }
   }
  }
 }
 req.send(null);
}

function openProgressBar() {
 /* generate random progress-id */
 uuid = "";
 for (i = 0; i &amp;lt; 32; i++) {
  uuid += Math.floor(Math.random() * 16).toString(16);
 }
 /* patch the form-action tag to include the progress-id */
 document.getElementById("upload").action="/upload.php?" + uuid;

 /* call the progress-updater every 1000ms */
 interval = window.setInterval(
   function () {
     fetch(uuid);
   },
   1000
 );
}

 &amp;lt;/script&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;Now I only have to commit this code to &lt;span class="caps"&gt;SVN&lt;/span&gt; and make a pre-release :)&lt;/p&gt;</description>
      <pubDate>Tue, 01 Aug 2006 16:54:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:fbeb29a2-28d9-4ff3-bc45-0ce3266b1076</guid>
      <author>jan</author>
      <link>http://blog.lighttpd.net/articles/2006/08/01/mod_uploadprogress-is-back</link>
      <trackback:ping>http://blog.lighttpd.net/articles/trackback/1887</trackback:ping>
    </item>
    <item>
      <title>"mod_uploadprogress is back" by dave</title>
      <description>I'd like to try this out, but I can't find the source in svn. Is it in the lighttpd-merge-1.4.x branch?</description>
      <pubDate>Tue, 22 Aug 2006 18:04:36 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a6f29cd9-028f-492f-a9eb-142af67b550f</guid>
      <link>http://blog.lighttpd.net/articles/2006/08/01/mod_uploadprogress-is-back#comment-1957</link>
    </item>
    <item>
      <title>"mod_uploadprogress is back" by hruske</title>
      <description>Boom, make sure you aren't rewriting your url or passing it to fastcgi.</description>
      <pubDate>Tue, 22 Aug 2006 10:17:39 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:823a7218-cbc3-4aae-9c84-6cfa4c8b0ec5</guid>
      <link>http://blog.lighttpd.net/articles/2006/08/01/mod_uploadprogress-is-back#comment-1955</link>
    </item>
    <item>
      <title>"mod_uploadprogress is back" by wiz</title>
      <description>And how to get it?...</description>
      <pubDate>Fri, 18 Aug 2006 07:36:59 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:bfb7e958-d478-4c90-90c3-f3f1eee47832</guid>
      <link>http://blog.lighttpd.net/articles/2006/08/01/mod_uploadprogress-is-back#comment-1939</link>
    </item>
    <item>
      <title>"mod_uploadprogress is back" by mod_uploadprogress-lover</title>
      <description>Thank YOU! :D</description>
      <pubDate>Sat, 12 Aug 2006 00:16:24 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:39b38693-60d1-4b37-b75f-814eb56e8bfc</guid>
      <link>http://blog.lighttpd.net/articles/2006/08/01/mod_uploadprogress-is-back#comment-1933</link>
    </item>
    <item>
      <title>"mod_uploadprogress is back" by Nick</title>
      <description>Sweetness!!!!!!!!!!

Any clues on how to test it out??</description>
      <pubDate>Fri, 11 Aug 2006 06:31:05 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:a8e4eebe-2466-4efc-a016-4d22d6099481</guid>
      <link>http://blog.lighttpd.net/articles/2006/08/01/mod_uploadprogress-is-back#comment-1930</link>
    </item>
    <item>
      <title>"mod_uploadprogress is back" by Boom</title>
      <description>I trying to test this module, but
req.open("GET", "/progress", 1);
return 404 error code

What's wrong?</description>
      <pubDate>Thu, 10 Aug 2006 16:42:12 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:39feecc2-f442-46b8-aba6-774f2701e4c9</guid>
      <link>http://blog.lighttpd.net/articles/2006/08/01/mod_uploadprogress-is-back#comment-1928</link>
    </item>
    <item>
      <title>"mod_uploadprogress is back" by fcicq</title>
      <description>Great!!!!
I'm waiting for it.....</description>
      <pubDate>Sun, 06 Aug 2006 04:41:36 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:3d73878a-f6ce-484a-84b2-38d0951073b4</guid>
      <link>http://blog.lighttpd.net/articles/2006/08/01/mod_uploadprogress-is-back#comment-1916</link>
    </item>
    <item>
      <title>"mod_uploadprogress is back" by johnny99</title>
      <description>That's absolutely fantastic, thanks so much. Been waiting and hoping this would re-appear.

Any idea when the pre-release might be available? Or, dare I ask, the general release?</description>
      <pubDate>Fri, 04 Aug 2006 19:28:39 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:41c67d22-7645-4413-892f-a06053a3ca6c</guid>
      <link>http://blog.lighttpd.net/articles/2006/08/01/mod_uploadprogress-is-back#comment-1913</link>
    </item>
    <item>
      <title>"mod_uploadprogress is back" by ARJ</title>
      <description>Yay! This is great. I like the straight-forwardness (is that a word?) We will definitely be making use of this.</description>
      <pubDate>Tue, 01 Aug 2006 19:39:21 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:ecbb7eb9-9899-49cf-a710-72c33391a6af</guid>
      <link>http://blog.lighttpd.net/articles/2006/08/01/mod_uploadprogress-is-back#comment-1890</link>
    </item>
    <item>
      <title>"mod_uploadprogress is back" by chernousov</title>
      <description>great!
doing svn up each few minutes, can't wait for your commit! ;))</description>
      <pubDate>Tue, 01 Aug 2006 17:39:57 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:7e05a409-2038-4b3f-b1ea-49b7bdaf8a76</guid>
      <link>http://blog.lighttpd.net/articles/2006/08/01/mod_uploadprogress-is-back#comment-1889</link>
    </item>
  </channel>
</rss>
