<?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>Kalen Gibbons &#187; FileStream</title>
	<atom:link href="http://www.kalengibbons.com/blog/index.php/tag/filestream/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kalengibbons.com/blog</link>
	<description>The Dead Tree Blog</description>
	<lastBuildDate>Mon, 24 Oct 2011 20:43:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to drag, drop, and parse a CSV file in AIR</title>
		<link>http://www.kalengibbons.com/blog/index.php/2008/11/how-to-drag-drop-and-parse-a-csv-file-in-air/</link>
		<comments>http://www.kalengibbons.com/blog/index.php/2008/11/how-to-drag-drop-and-parse-a-csv-file-in-air/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 17:27:09 +0000</pubDate>
		<dc:creator>Kalen Gibbons</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[CSV]]></category>
		<category><![CDATA[drag]]></category>
		<category><![CDATA[drop]]></category>
		<category><![CDATA[FileStream]]></category>
		<category><![CDATA[NativeDragEvent]]></category>
		<category><![CDATA[NativeDragManager]]></category>
		<category><![CDATA[parse]]></category>

		<guid isPermaLink="false">http://www.kalengibbons.com/blog2/?p=61</guid>
		<description><![CDATA[I&#8217;ve been playing around with Adobe AIR lately and I&#8217;ve really been enjoying it. So I thought I&#8217;d put out this quick tutorial on how to drag and drop a file into your AIR application; in this case we will be using a CSV file, and then we will parse that file so that its [...]]]></description>
			<content:encoded><![CDATA[<p><img style="float: left; border: 0; margin-top: 0px; margin-bottom: 0px; margin-left: 0px; margin-right: 15px;" src="/assets/images/csvDrag.png" alt="Drag and Drop a CSV file in Adobe AIR" width="80" height="84" />I&#8217;ve been playing around with Adobe AIR lately and I&#8217;ve really been enjoying it.  So I thought I&#8217;d put out this quick tutorial on how to drag and drop a file into your AIR application; in this case we will be using a CSV file, and then we will parse that file so that its data can be used in a datagrid.  You can <a title="Download source files" href="http://www.kalengibbons.com/assets/files/CSVDragandDrop.zip" target="_blank">download the full source code here</a>.</p>
<h3 class="accentBold">Dragging a file into AIR:</h3>
<p>First, we need to set up handlers to listen for the drag events.  We are going to set up two listeners, one for when an item is dragged over the app and one for when that item is dropped.</p>
<p class="code">
<span class="asReserved">this</span>.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, dragEnterHandler);<br />
<span class="asReserved">this</span>.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, dragDropHandler);
</p>
<p>Then we&#8217;ll set up the two handler function.  The dragEnterHandler function determines if the item is an acceptable file type.  You could use the different constants of the <a title="Flex 3 Clipboard class" href="http://livedocs.adobe.com/flex/3/langref/flash/desktop/Clipboard.html" target="_blank">ClipboardFormats</a> class to only accept specific file formats, but for now we will accept any valid file.</p>
<div class="code">
<span class="asReserved">private</span> <span class="asFunction">function</span> dragEnterHandler(evt:NativeDragEvent):<span class="asReserved">void</span>{</p>
<div class="indent">
	<span class="asReserved">if</span>(evt.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT)){</p>
<div class="indent">
		NativeDragManager.acceptDragDrop(<span class="asReserved">this</span>);
</div>
<p>}
</div>
<p>}
</div>
<p>Next, when the user drops the file, we will copy it from the clipboard, read its contents, and parse its values into a more usable data structure.</p>
<div class="code">
<span class="asReserved">private</span> <span class="asFunction">function</span> dragDropHandler(evt:NativeDragEvent):<span class="asReserved">void</span>{</p>
<div class="indent">
	NativeDragManager.dropAction = NativeDragActions.COPY;<br />
	<span class="asComment">//get an array of the files dropped in</span><br />
	<span class="asVar">var</span> dropFiles:Array = evt.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) <span class="asReserved">as</span> Array;<br />
	<span class="asComment">//get the content of the file</span><br />
	<span class="asVar">var</span> fileContent:String = getFileContent(dropFiles[0]);<br />
	<span class="asComment">//parse it to get an array of Orders</span><br />
	orders = parseCSV(fileContent);
</div>
<p>}
</div>
</p>
<h3 class="accentBold">Reading the content of the dropped file:</h3>
<p>For this we will use the <a title="Flex 3 FileStream class" href="http://livedocs.adobe.com/flex/3/langref/flash/filesystem/FileStream.html" target="_blank">FileStream</a> class.  The getFileContent function takes a generic file as its only parameter; it opens a FileStream, reads the contents of the file and returns that value as a string.</p>
<div class="code">
<span class="asReserved">private</span> <span class="asFunction">function</span> getFileContent(_file:File):String{</p>
<div class="indent">
	<span class="asComment">//open a fileStream to read the content of the file</span><br />
	<span class="asVar">var</span> fileStream:FileStream = <span class="asReserved">new</span> FileStream();<br />
	fileStream.open(_file, FileMode.READ);<br />
	<span class="asVar">var</span> fileContent:String = fileStream.readUTFBytes(fileStream.bytesAvailable);<br />
	fileStream.close();<br />
	<span class="asReserved">return</span> fileContent;
</div>
<p>}
</div>
</p>
<h3 class="accentBold">Parsing the CSV file:</h3>
<p>Finally, we are going to take the content of the CSV file, which is now in the form of a string, and parse out its data to populate an Array.  We start by using the split() function to create an Array of line items. We use character code 10, which represents carriage returns, and character code 13, which represents new lines. to separate each line item.  Then we loop through the newly created array of line items and pull out each comma separated value.  At this point we have the information we need to populate the Order object and add that to our Array.</p>
<div class="code">
<span class="asReserved">private</span> <span class="asFunction">function</span>  parseCSV(_content:String):Array{</p>
<div class="indent">
	<span class="asComment">//create temporary array to store the Orders</span><br />
	<span class="asVar">var</span> csvArray:Array = <span class="asReserved">new</span> Array();<br />
	<span class="asComment">//break the csv into individual lines</span><br />
	<span class="asVar">var</span> csvLines:Array = _content.split(String.fromCharCode(13,10));<br />
	<span class="asComment">//remove title row</span><br />
	csvLines.splice(0,1);<br />
	<span class="asComment">//loop over each line</span><br />
	<span class="asReserved">for each</span>(<span class="asVar">var</span> s:String <span class="asReserved">in</span> csvLines){</p>
<div class="indent">
		<span class="asVar">var</span> lineItems:Array = s.split(<span class="asString">&#8220;,&#8221;</span>);<br />
		<span class="asVar">var</span> transaction:Order = <span class="asReserved">new</span> Order(	lineItems[0],</p>
<div class="indent6">
<div class="indent5">
							<span class="asReserved">new</span> Date(lineItems[1]),<br />
							lineItems[2],<br />
							lineItems[3],<br />
							lineItems[4]);
</div>
</div>
<p>csvArray.push(transaction);
</div>
<p>}<br />
	<span class="asReserved">return</span> csvArray;
</div>
<p>}
</div>
<p>The data pulled from the CSV file is now in a more manageable form and can be used as a dataProvider for components or to populate AIR&#8217;s built in SQLite database.  Of course, in this tutorial, for simplicity&#8217;s sake, we assume that the file dropped into the application is a CSV file.  In your application you should have some trapping to determine the file type and process it accordingly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kalengibbons.com/blog/index.php/2008/11/how-to-drag-drop-and-parse-a-csv-file-in-air/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

