<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IONCANNON &#187; ajax</title>
	<atom:link href="http://www.ioncannon.net/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ioncannon.net</link>
	<description>Thoughts on Software Development and Engineering</description>
	<lastBuildDate>Tue, 03 Jan 2012 13:59:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<atom:link rel='hub' href='http://www.ioncannon.net/?pushpress=hub'/>
		<item>
		<title>Range Requests with Ajax</title>
		<link>http://www.ioncannon.net/programming/1506/range-requests-with-ajax/</link>
		<comments>http://www.ioncannon.net/programming/1506/range-requests-with-ajax/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 16:03:58 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/?p=1506</guid>
		<description><![CDATA[I ran across something the other day that made wonder about doing range requests using ajax. For some reason it wasn&#039;t obvious at first if this would be easy but as it turns out it is. If you aren&#039;t familiar with range requests head over to the HTTP RFC and check out the range header. [...]]]></description>
			<content:encoded><![CDATA[<p>I ran across something the other day that made wonder about doing range requests using ajax. For some reason it wasn&#039;t obvious at first if this would be easy but as it turns out it is.</p>
<p>If you aren&#039;t familiar with range requests head over to the HTTP RFC and check out the <a href="http://tools.ietf.org/html/rfc2616#section-14.35.2">range header</a>. Your web server needs to support range requests for this to be useful but most do so that shouldn&#039;t be a huge issue. As a bonus you will find that some CDNs support range request as well (Amazon S3 for example).</p>
<p><span id="more-1506"></span></p>
<p>To show how this works I&#039;ll start off by getting the offsets of a text file, then use curl to show the actual request and then put it into an ajax request. Even though I&#039;m using a text file the same thing could be done with a binary file as well. Here are the contents of the file I&#039;ve called range-test.txt:</p>
<pre class="brush: plain; title: ; notranslate">
First part of the file.

More text here.

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Some text after Lorem Ipsum.

End of the file.
</pre>
<p>Using egrep we can get the offset of each line pretty easy like so:</p>
<pre class="brush: plain; title: ; notranslate">
&gt; egrep -b &quot;\r|\n&quot; range-test.txt
0:First part of the file.
25:More text here.
42:Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
618:Some text after Lorem Ipsum.
660:End of the file.
</pre>
<p>Quickly verify that the range looks right in isolation using hexdump:</p>
<pre class="brush: plain; title: ; notranslate">
&gt; hexdump -c -s 618 -n 30 range-test.txt
000026a   S   o   m   e       t   e   x   t       a   f   t   e   r
000027a   L   o   r   e   m       I   p   s   u   m   .  \n  \n
0000288
</pre>
<p>Now we have a sample range so put the text file on your web server and use curl to send the range request headers (note that the hexdump takes an offset and count of bytes while the range header takes an start and end offset):</p>
<pre class="brush: plain; title: ; notranslate">
&gt; curl -H 'Range:bytes=618-647' http://localhost/range-test.txt
Some text after Lorem Ipsum.
</pre>
<p>Now that it makes sense how this range is used to make a request for part of a file we can move on to doing it with an ajax request. I&#039;m going to use <a href="http://jquery.com/">jQuery</a>&#039;s <a href="http://api.jquery.com/jQuery.ajax/">ajax</a> function to make this really simple. It has a feature that lets you set headers really easily. The following is all you need:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;html&gt;
  &lt;head&gt;
    &lt;script type=&quot;text/javascript&quot; src=&quot;jquery-1.7.min.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
    $(function() {
      $.ajax({
        url: 'range-test.txt',
        headers: {Range: &quot;bytes=618-647&quot;},
        success: function( data ) { $('#results').html( data ); }
      });
    });
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;results&quot;&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>And that is all there is to it. This could be useful for all kinds of things like displaying just a section of a large log file at a time. This might also be useful in a situation where you want to stream binary data back to the browser a chunk at a time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/programming/1506/range-requests-with-ajax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Upgrade to PHP 5.2 and Get JSON For Free</title>
		<link>http://www.ioncannon.net/programming/103/upgrade-to-php-52-and-get-json-for-free/</link>
		<comments>http://www.ioncannon.net/programming/103/upgrade-to-php-52-and-get-json-for-free/#comments</comments>
		<pubDate>Tue, 14 Nov 2006 16:19:20 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/php/103/upgrade-to-php-52-and-get-json-for-free/</guid>
		<description><![CDATA[A few days ago when PHP 5.2 was released one of the things that caught my eye was that it now includes the JSON extension. For anyone doing AJAXy type stuff JSON is an easy way to martial your data between your server side language and javascript. For the longest time I&#039;ve been using the [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago when <a href="http://www.php.net/releases/5_2_0.php">PHP 5.2 was released</a> one of the things that caught my eye was that it now includes the JSON extension. For anyone doing AJAXy type stuff <a href="http://json.org/">JSON</a> is an easy way to martial your data between your server side language and javascript.</p>
<p>For the longest time I&#039;ve been using the <a href="http://mike.teczno.com/json.html">older PHP JSON library</a> to do JSON with PHP but now that the extension is included in the core I decided it was time to test it out. I took a couple of minutes and converted my <a href="http://www.ioncannon.net/dnsbl/">DNSBL checker</a> as a test since it has a fairly large data-set that gets converted and sent back. The <a href="http://us2.php.net/json">json functions</a> provided by the extension are probably easier to use since the JSON library needed you to create an object first but that wasn&#039;t a real issue. After making the change I could tell JSON extension was faster than the library. After a little digging I found that someone has done a little <a href="http://www.aurore.net/projects/php-json/">extension vs library</a> testing and claims the JSON extension is 153 times as fast as the library.</p>
<p>Tags: <a href="http://technorati.com/tag/php" rel="tag">php</a>, <a href="http://technorati.com/tag/ajax" rel="tag"> ajax</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/programming/103/upgrade-to-php-52-and-get-json-for-free/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some interesting and useful AJAX/Javascript code</title>
		<link>http://www.ioncannon.net/web-design/63/some-interesting-and-useful-ajaxjavascript-code/</link>
		<comments>http://www.ioncannon.net/web-design/63/some-interesting-and-useful-ajaxjavascript-code/#comments</comments>
		<pubDate>Fri, 23 Jun 2006 15:10:57 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[web design]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/web-design/63/some-interesting-and-useful-ajaxjavascript-code/</guid>
		<description><![CDATA[I like seeing more and more uses of prototype. I&#039;m not sure if the big guys will win out with their UI toolkits (Yahoo UI/GWT) or if it will always feel better to put things together by hand. Either way it is good to understand how this stuff works. This is an edit in place [...]]]></description>
			<content:encoded><![CDATA[<p>I like seeing more and more uses of prototype. I&#039;m not sure if the big guys will win out with their UI toolkits (Yahoo UI/GWT) or if it will always feel better to put things together by hand. Either way it is good to understand how this stuff works. This is an <a href="http://joseph.randomnetworks.com/archives/2006/06/07/ajax-edit-in-place-with-prototype-version-020/">edit in place</a> example that is similar to what you see on flickr.</p>
<p>I&#039;ve seen something like this a number of times when I&#039;ve visited sites. They want your feedback on something or other while you are browsing. It is a little floating plus feedback sign that hangs out in the lower right hand corner. <a href="http://www.ibegin.com/blog/p_ajax_feedback_mechanism.html">Check it out</a>.</p>
<p>Browsing digg I noticed an article on &#034;unobtrusive sidenotes&#034; and found that the idea is pretty cool: The <a href="http://blog.arc90.com/2006/05/introducing_unobtrusive_sideno.php">announcement</a> and <a href="http://lab.arc90.com/2006/05/unobtrusive_sidenotes.php">code</a>. They are done with javascript so you can turn them on and off on the fly.</p>
<p>Tags: <a href="http://technorati.com/tag/javascript" rel="tag">javascript</a>, <a href="http://technorati.com/tag/ajax" rel="tag"> ajax</a>, <a href="http://technorati.com/tag/sidenote" rel="tag"> sidenote</a>, <a href="http://technorati.com/tag/ajax+feedback" rel="tag"> ajax feedback</a>, <a href="http://technorati.com/tag/prototype" rel="tag"> prototype</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/web-design/63/some-interesting-and-useful-ajaxjavascript-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AJAX file upload progress for Java using commons fileupload and prototype</title>
		<link>http://www.ioncannon.net/programming/38/ajax-file-upload-progress-for-java-using-commons-fileupload-and-prototype/</link>
		<comments>http://www.ioncannon.net/programming/38/ajax-file-upload-progress-for-java-using-commons-fileupload-and-prototype/#comments</comments>
		<pubDate>Mon, 03 Apr 2006 02:15:15 +0000</pubDate>
		<dc:creator>carson</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.ioncannon.net/uncategorized/38/ajax-file-upload-progress-for-java-using-commons-fileupload-and-prototype/</guid>
		<description><![CDATA[This has been done before with PHP (AJAX upload progress meter for PHP) etc but I needed something a little different because I wanted to upload a file and then have it loaded into a database. I looked around and found that someone had already made something that used the commons file upload package to [...]]]></description>
			<content:encoded><![CDATA[<p>This has been done before with PHP (<a href="http://bluga.net/projects/uploadProgressMeter/">AJAX upload progress meter for PHP</a>) etc but I needed something a little different because I wanted to upload a file and then have it loaded into a database. I looked around and found that someone had already made something that used the commons file upload package to do the upload part (<a href="http://www.telio.be/blog/2006/01/06/ajax-upload-progress-monitor-for-commons-fileupload-example/">AJAX Upload progress monitor for Commons-FileUpload Example</a>). It wasn&#039;t exactly what I was looking for but it a good start.</p>
<p><span id="more-38"></span></p>
<p>To understand the way this works I think it is easiest to break it down into parts:</p>
<ol>
<li>A file upload extention that counts bytes as they are uploaded</li>
<li>An interface that monitors the progress of something running on the server</li>
<li>AJAX to pull the monitoring into the current screen</li>
</ol>
<p><a></a></p>
<h3>Counting bytes when files are uploaded</h3>
<p>This was taken from the <a href="http://www.telio.be/blog/2006/01/06/ajax-upload-progress-monitor-for-commons-fileupload-example/">example listed above</a>. It extends and wraps parts of the commons File Upload classes so that you can count the bytes as they are uploaded to the server. You can download the <a href="http://www.ioncannon.net/examples/fileupload-ext.zip">source with build file</a> or the <a href="http://www.ioncannon.net/examples/fileupload-ext1.zip">binary</a>. You will also need the <a href="http://jakarta.apache.org/site/downloads/downloads_commons-fileupload.cgi">commons file upload</a>, <a href="http://jakarta.apache.org/site/downloads/downloads_commons-io.cgi">commons io</a> and <a href="http://jakarta.apache.org/site/downloads/downloads_commons-logging.cgi">commons logging</a>. If you download the source put the commons jars in the lib directory before building.</p>
<p>The code is fairly simple to follow. MonitoredDiskFileItemFactory replaces DiskFileItemFactory and the construction of a MonitoredDiskFileItemFactory takes a OutputStreamListener that will be passed on down the chain. The new factory creates MonitoredDiskFileItems instead of DiskFileItems for each file uploaded. When the file needs to be written to disk a MonitoredOutputStream is given back instead of a normal OutputStream. The MonitoredOutputStream calls the OutputStreamListener methods as the bytes are written and with that you now have a way to monitor the byte count as the file is created on the server.</p>
<p>Now to test this all out we can just have an OutputStreamListener that writes its progress out to a logfile or something.</p>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="kw1">public</span> <span class="kw1">class</span> FileUploadListener <span class="kw1">implements</span> OutputStreamListener<br />
<span class="br0">&#123;</span><br />
&nbsp; <span class="kw1">private</span> <span class="kw4">long</span> totalFileSize<span class="sy0">;</span><br />
&nbsp; <span class="kw1">private</span> <span class="kw4">long</span> currentFileRead<span class="sy0">;</span></p>
<p>&nbsp; <span class="kw1">public</span> FileUploadListener<span class="br0">&#40;</span><span class="kw4">long</span> totalFileSize<span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">totalFileSize</span> <span class="sy0">=</span> totalFileSize<span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">currentFileRead</span> <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw1">public</span> <span class="kw4">void</span> start<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; log.<span class="me1">debug</span><span class="br0">&#40;</span><span class="st0">&quot;Upload started. Total file size: &quot;</span> <span class="sy0">+</span> totalFileSize<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw1">public</span> <span class="kw4">void</span> bytesRead<span class="br0">&#40;</span><span class="kw4">int</span> byteCount<span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; log.<span class="me1">debug</span><span class="br0">&#40;</span><span class="st0">&quot;Read bytes. Currently &quot;</span> <span class="sy0">+</span> byteCount <span class="sy0">+</span> <span class="st0">&quot; out of &quot;</span> <span class="sy0">+</span> totalFileSize <span class="sy0">+</span> <span class="st0">&quot; bytes.&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; currentFileRead<span class="sy0">+=</span>byteCount<span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw1">public</span> <span class="kw4">void</span> error<span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">String</span></a> error<span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; log.<span class="me1">debug</span><span class="br0">&#40;</span><span class="st0">&quot;Hit an error: &quot;</span> <span class="sy0">+</span> error<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw1">public</span> <span class="kw4">void</span> done<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; log.<span class="me1">debug</span><span class="br0">&#40;</span><span class="st0">&quot;Upload done.&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw1">public</span> <span class="kw4">long</span> getTotalRead<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> currentFileRead<span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; <span class="kw1">public</span> <span class="kw4">long</span> getTotalSize<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> totalFileSize<span class="sy0">;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Now we try it out. You can put this in a servlet or jsp so I&#039;m only going to list the parts that matter.</p>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;">FileUploadListener listener <span class="sy0">=</span> <span class="kw1">new</span> FileUploadListener<span class="br0">&#40;</span>request.<span class="me1">getContentLength</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; session.<span class="me1">setAttribute</span><span class="br0">&#40;</span><span class="st0">&quot;LISTENER&quot;</span>, listener<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; FileItemFactory factory <span class="sy0">=</span> <span class="kw1">new</span> MonitoredDiskFileItemFactory<span class="br0">&#40;</span>listener<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; ServletFileUpload upload <span class="sy0">=</span> <span class="kw1">new</span> ServletFileUpload<span class="br0">&#40;</span>factory<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alist+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">List</span></a> items <span class="sy0">=</span> upload.<span class="me1">parseRequest</span><span class="br0">&#40;</span>request<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aiterator+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span class="kw3">Iterator</span></a> i <span class="sy0">=</span> items.<span class="me1">iterator</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> i.<span class="me1">hasNext</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><span class="br0">&#41;</span><br />
&nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; FileItem fileItem <span class="sy0">=</span> <span class="br0">&#40;</span>FileItem<span class="br0">&#41;</span> i.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="sy0">!</span>fileItem.<span class="me1">isFormField</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">// code here to process the file</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp;<span class="br0">&#125;</span></div>
</div>
<p>I&#039;m going to assume you can find the correct way to do the actual form upload part.</p>
<p>Note: One issue that you will face at some point is where the upload post goes to becuase when you get to the AJAXy part of things you want the post to stay on the same page. You can use a hidden iframe and the form&#039;s &#034;target&#034; parameter to do this (I have an example later). This is one thing the Java examples I found didn&#039;t have but the PHP examples did and I&#039;m not sure exactly how the Java examples work without it.</p>
<h3>Monitoring progress on the server</h3>
<p>The next step is to monitor the progress of the upload on the server. What you are monitoring on the server doesn&#039;t even need to be the upload. For the work I was doing the upload goes fairly quickly but what happens to the file after the upload takes a little longer. I wanted to monitor both and that is one reason I think it helps to break this up into parts because you aren&#039;t limited to just monitoring file uploads.</p>
<p>The main thing to keep in mind here is that the application server is multithreaded and you can make more than one request to the server at the same time. You probably know that you can open a tab in firefox or another window in ie and use the same session from the current webapp you are using. Knowing that you can create a page that monitors the status of things as they are running on the server. </p>
<p>From the example above you could toss the listener into the users session. Then insead of logging you just add a couple variables to keep track of the number of bytes that have been uploaded. Then create a simple jsp that pulls the Listener out of the session and dumps its data to a page. Open two windows, one to the upload page and another one to the status page. Start the upload and then start refreshing the status. You should see that the values change as the file is uploaded.</p>
<div class="codesnip-container" >
<div class="java codesnip" style="font-family:monospace;"><span class="sy0">&lt;%</span>@page<span class="sy0">%&gt;</span><br />
<span class="sy0">&lt;%</span><br />
&nbsp; FileUploadListener listener <span class="sy0">=</span> <span class="br0">&#40;</span>FileUploadListener<span class="br0">&#41;</span>session.<span class="me1">getAttribute</span><span class="br0">&#40;</span><span class="st0">&quot;LISTENER&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy0">%&gt;</span><br />
Total size<span class="sy0">:</span> <span class="sy0">&lt;%=</span>listener.<span class="me1">getTotalSize</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">%&gt;&lt;</span>br<span class="sy0">/&gt;</span><br />
Read count<span class="sy0">:</span> <span class="sy0">&lt;%=</span>listener.<span class="me1">getTotalRead</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">%&gt;&lt;</span>br<span class="sy0">/&gt;</span></div>
</div>
<p>Of course you will probably want more than just the total size and bytes read as well as more formating like a little progress bar or something but I&#039;ll leave that up to you. </p>
<h3>AJAX integration with prototype</h3>
<p>You have the major parts to the upload progress done and now all you need is the AJAX part. To do this I chose to use <a href="http://prototype.conio.net/">prototype</a> because it cuts right to what you want to do. One call is all you need to use: Ajax.PeriodicalUpdater.</p>
<p>The Ajax.PeriodicalUpdater call will update a container (in my case a div) on a set interval. Here is an example of how to have it update a div with an id of &#034;status&#034; every second.</p>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;"><span class="kw2">new</span> Ajax.<span class="me1">PeriodicalUpdater</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#039;status&#039;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&#039;status.jsp&#039;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>asynchronous<span class="sy0">:</span><span class="kw2">true</span><span class="sy0">,</span> frequency<span class="sy0">:</span><span class="nu0">1</span><span class="sy0">,</span> method<span class="sy0">:</span><span class="st0">&#039;get&#039;</span><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
<p>The first argument is the id of the div, the second is the jsp that contains the data to stick into the div every second and the 3rd arguement is a set of options. There are more options availabe if you need them.</p>
<p>You would want to kick the update off whenever the form is posted. When the post is complete the iframe used as a place to post to will load with the results of the servlet or jsp that you posted to. If you return some javascript as a result for the iframe you will be able to create a final &#034;finished&#034; message on the page to let the user know the upload has completed and stop the processing of the AJAX updater.</p>
<p>So there you have it. The basics of setting up an upload progress bar using java and AJAX. I have left out a good bit but you should have enough to at least get you started.</p>
<p>By request I have created a simple example that pulls everything together. The source contains everything you need to create a war file including all source and an ant build file.</p>
<p><a href="http://www.ioncannon.net/examples/testupload.zip">Example Source</a>
</p>
<p>Tags: <a href="http://technorati.com/tag/java" rel="tag">java</a>, <a href="http://technorati.com/tag/ajax" rel="tag"> ajax</a>, <a href="http://technorati.com/tag/file+upload" rel="tag"> file upload</a>, <a href="http://technorati.com/tag/prototype" rel="tag"> prototype</a>, <a href="http://technorati.com/tag/javascript" rel="tag"> javascript</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ioncannon.net/programming/38/ajax-file-upload-progress-for-java-using-commons-fileupload-and-prototype/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

