<?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>Kevin Musselman &#187; file upload</title>
	<atom:link href="http://kevinmusselman.com/blog/tag/file-upload/feed/" rel="self" type="application/rss+xml" />
	<link>http://kevinmusselman.com/blog</link>
	<description>Some helpful tutorials</description>
	<lastBuildDate>Tue, 19 Jan 2010 16:27:08 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Multiple File Upload Using Flash</title>
		<link>http://kevinmusselman.com/blog/2009/01/multiple-file-upload-using-flash/</link>
		<comments>http://kevinmusselman.com/blog/2009/01/multiple-file-upload-using-flash/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 00:53:44 +0000</pubDate>
		<dc:creator>kmussel</dc:creator>
				<category><![CDATA[tutorial]]></category>
		<category><![CDATA[file upload]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://kevinmusselman.com/blog/?p=3</guid>
		<description><![CDATA[
I came across this issue recently when I was building a website where I wanted users to be able to upload multiple photos.  Since they were allowed to upload as many photos as they wanted I thought it would be nice for them not to have to do it one by one.  So [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
     function showhideCode(){
         if(document.getElementById('code').style.display == 'block'){
           document.getElementById('codetext').innerHTML = 'Show Code';
           document.getElementById('code').style.display = 'none';
         }else {
                document.getElementById('code').style.display = 'block';
                document.getElementById('codetext').innerHTML = 'Hide Code';
          }
     }
// --></script><br />
I came across this issue recently when I was building a website where I wanted users to be able to upload multiple photos.  Since they were allowed to upload as many photos as they wanted I thought it would be nice for them not to have to do it one by one.  So I began looking around to figure out the best way to do this.<br />
I came across a few options to do this.</p>
<ul style="list-style-type:upper-alpha;">
<li> I could spend a few hundred dollars on a java applet.  Though this is a nice application that lets you view and select the files you want, it is a little expensive when you just want the functionality of uploading multiple files. Or you could build your own. Though for me, I think buying one is cheaper than spending the time on building one.</li>
<li> I could use flash to turn my regular browse dialog box into a multiple file upload dialog box.</li>
</ul>
<p>For now, I decided to go with option B. At some point I might add option A to the site but it will still be nice to have the Flash based upload application for those people that don&#8217;t have java installed. And if they don&#8217;t have flash installed I&#8217;ll make sure they can upload one file at a time as well.</p>
<p>So this is how I accomplished it.</p>
<p>First off I am using Flash 8 and actionscript 2</p>
<div style='margin-top:5px; margin-bottom:5px;'>
Create a new Flash document and add a button.  Originally I didn&#8217;t add a button and used a regular html button to open the browse dialog box.  This worked fine unless you have Flash 10 installed which has extra security features which don&#8217;t allow that.  So I had to add a button in flash to get around it.
</div>
<p>After you create the button go back to the root and add everything there.<br/><br />
Here is what we need to include:</p>
<div><code style="width: 100px;">import flash.net.FileReferenceList;</code> (this is to actually select multiple files at once)</div>
<div><code style="width: 100px;">import flash.net.FileReference; </code> (this is the individual file that is being uploaded)</div>
<div><code style="width: 100px;">import flash.external.ExternalInterface;</code> (I included this because I like to be able to call javascript functions in flash and flash functions in javascript.  But it is not needed to upload multiple files.)</div>
<p><br/><br />
I create two listener objects, one for the FileReferenceList and one for the FileReference.<br />
<code><br />
var listener:Object = new Object();<br />
var listener2:Object = new Object();<br />
</code></p>
<p>I then create a few variables:</p>
<div><code>var itemnum = 0; </code> // This is the current file position in the fileList array</div>
<div><code>var numfiles = 0; </code> //  How many files are being uploaded</div>
<div><code>var list:Array; </code> // This is what we will store the files being uploaded into</div>
<p><br/><br />
This is the onSelect event that is fired after selecting file(s) in the browse dialog box and then hitting the Select button.</p>
<pre>listener.onSelect = function(fileRefList:FileReferenceList) {
	list = fileRefList.fileList;
	numfiles = list.length;
	uploadItem(itemnum);   // function to actually upload the photos
}</pre>
<p><br/><br />
This event is called after each photo selected has been uploaded.  It also calls a javascript function &#8220;showpics&#8221; which is an ajax call to display each photo after it has been uploaded.</p>
<pre>listener2.onComplete = function(){
	itemnum += 1;
	ExternalInterface.call("showpics");
	if(itemnum&lt;numfiles)
		uploadItem(itemnum);  //call function on next photo
}</pre>
<p><br/><br />
This is the function to actually upload the file .  The file you send it to should be a relative link otherwise flash 10 will throw a security error.</p>
<pre>
function uploadItem(num:Number){
	var item:FileReference = list[num];
	item.addListener(listener2);
	item.upload("yourfile.php");
}</pre>
<p><br/><br />
We need to create an array of the types of files that we want to to be able to select from the browse dialog box.</p>
<pre>var allTypes:Array = new Array();
var imageTypes:Object = new Object();
imageTypes.description = "Images (*.JPG;*.JPEG;*.JPE;*.GIF;*.PNG;)";
imageTypes.extension = "*.jpg; *.JPG; *.jpeg; *.jpe; *.gif; *.png;";
allTypes.push(imageTypes);</pre>
<p><br/><br />
Finally, this is the function that will actually display the browse dialog box.</p>
<pre>function uploadFile(){
	var fileRef:FileReferenceList = new FileReferenceList();
	fileRef.addListener(listener);
	fileRef.browse(allTypes);
}</pre>
<p><br/><br />
The next part is to add the onClick handler to the button.  Make sure you set the variable name of the button to something; in this case &#8220;btn&#8221;.<br />
<code><br />
btn.onClick = function(){<br />
  uploadpics();<br />
}<br />
</code><br />
This next part allows me to use a simple input of type button to call the uploadFile function unless the user has flash 10 installed and then this wont work.</p>
<p><code>ExternalInterface.addCallback("javascript_function_name", this, "uploadFile"); </code></p>
<p>And that is it. Nice and simple.</p>
<p>* Note:  Apparently if you&#8217;re using a Mac OS the onComplete event doesn&#8217;t work unless you echo &#8220;1&#8243; at the end of the file you passed the image to. This way Macs know the file finished uploading.</p>
<p>**Also note that there is an even simpler and quicker way if all you want to do is upload the images without changing them.</p>
<p>To do it that way just change the following code:</p>
<pre>listener.onSelect = function(fileRefList:FileReferenceList) {
    list = fileRefList.fileList;
    var item:FileReference;
    numfiles = list.length;
    for(var i=0; i&lt;numfiles; i++){
        item = list[i];
  	item.upload("yourfile.php");
    }
}</pre>
<p>I didn&#8217;t do it this way because I am creating thumbnails of each image and needed each image to be done before the next image is uploaded.<br />
<br/><br />
<a href="http://www.kevinmusselman.com/examples/mupload.php" target='_new'>View Example</a></p>
<p><a href="http://www.kevinmusselman.com/examples/force_download.php?file=example_mupload.fla">Download FLA File</a></p>
<p><span id="codetext" style="cursor:pointer; color:#0000ff;" onclick="showhideCode();">Show  Code</span></p>
<div id="code" style="display:none">
<p><code><br />
import flash.net.FileReferenceList;<br />
import flash.net.FileReference;<br />
import flash.external.ExternalInterface;</code></p>
<p>var listener:Object = new Object();<br />
var listener2:Object = new Object();</p>
<p>var itemnum = 0;<br />
var numfiles = 0;<br />
var list:Array;</p>
<p>listener2.onComplete = function(){<br />
itemnum += 1;<br />
ExternalInterface.call(&#8221;showpics&#8221;);<br />
if(itemnum<br />
uploadItem(itemnum);<br />
}<br />
}</p>
<p>listener.onSelect = function(fileRefList:FileReferenceList) {<br />
list = fileRefList.fileList;<br />
numfiles = list.length;<br />
uploadItem(itemnum);<br />
}</p>
<p>function uploadItem(num:Number){<br />
item = list[num];<br />
item.addListener(listener2);<br />
var val = item.upload(&#8221;yourfile.php&#8221;);<br />
}</p>
<p>var allTypes:Array = new Array();<br />
var imageTypes:Object = new Object();<br />
imageTypes.description = &#8220;Images (*.JPG;*.JPEG;*.JPE;*.GIF;*.PNG;)&#8221;;<br />
imageTypes.extension = &#8220;*.jpg; *.JPG; *.jpeg; *.jpe; *.gif; *.png;&#8221;;<br />
allTypes.push(imageTypes);</p>
<p>ExternalInterface.addCallback(&#8221;uploadpics&#8221;, this, uploadpics);<br />
btn.onClick = function(){<br />
  uploadpics();<br />
}<br />
function uploadpics(){<br />
var fileRef:FileReferenceList = new FileReferenceList();<br />
fileRef.addListener(listener);<br />
fileRef.browse(allTypes);<br />
}</p></div>
]]></content:encoded>
			<wfw:commentRss>http://kevinmusselman.com/blog/2009/01/multiple-file-upload-using-flash/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
