<?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>zedia flash blog &#187; ByteArray</title>
	<atom:link href="http://www.zedia.net/tag/bytearray/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zedia.net</link>
	<description>Flash, ActionScript, SEO and everything in between</description>
	<lastBuildDate>Wed, 21 Jul 2010 17:07:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Sending byteArray (image) and variables to server-side script as POST data in AS3</title>
		<link>http://www.zedia.net/2010/sending-bytearray-image-and-variables-to-server-side-script-as-post-data-in-as3/</link>
		<comments>http://www.zedia.net/2010/sending-bytearray-image-and-variables-to-server-side-script-as-post-data-in-as3/#comments</comments>
		<pubDate>Wed, 21 Jul 2010 17:07:49 +0000</pubDate>
		<dc:creator>zedia.net</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[2007]]></category>
		<category><![CDATA[as3corelib]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[GET]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[Jonathan Marston]]></category>
		<category><![CDATA[POST]]></category>
		<category><![CDATA[server-side]]></category>
		<category><![CDATA[UploadPostHelper]]></category>

		<guid isPermaLink="false">http://www.zedia.net/?p=687</guid>
		<description><![CDATA[A couple things here, I first talked about how to send an image and variables to the server (at the same time) on this post, but it felt a bit weird, the file had no name to retrieve it and the variables were sent as GET. Now while working a with a new colleague, he showed [...]]]></description>
			<content:encoded><![CDATA[<p>A couple things here, I first talked about how to send an image and variables to the server (at the same time) <a title="Sending byteArray and variables to server-side script at the same time" href="http://www.zedia.net/2008/sending-bytearray-and-variables-to-server-side-script-at-the-same-time/" target="_blank">on this post</a>, but it felt a bit weird, the file had no name to retrieve it and the variables were sent as GET. Now while working a with a new colleague, he showed me this code that for him was nothing but for me was amazing. It is a little library called UploadPostHelper made by <a title="Jonathan Marston" href="http://marstonstudio.com/" target="_blank">Jonathan Marston</a> back in 2007 (2007! why didn&#8217;t someone talk to me about this library before!!!).</p>
<p>Anyway here is a little code snippet showing how to use it:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> jpegEncoder:JPGEncoder = <span style="color: #000000; font-weight: bold;">new</span> JPGEncoder<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">85</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//using the JPGEncoder from as3corelib</span>
<span style="color: #000000; font-weight: bold;">var</span> jpegBytes:ByteArray = jpegEncoder.<span style="color: #006600;">encode</span><span style="color: #66cc66;">&#40;</span>myPicture.<span style="color: #006600;">bitmapData</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//encoding a Bitmap into a ByteArray</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> urlRequest : URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
urlRequest.<span style="color: #0066CC;">url</span> = <span style="color: #ff0000;">&quot;THE URL OF YOUR SERVER SIDE SCRIPT&quot;</span>;
urlRequest.<span style="color: #0066CC;">contentType</span> = <span style="color: #ff0000;">'multipart/form-data; boundary='</span> + UploadPostHelper.<span style="color: #006600;">getBoundary</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
urlRequest.<span style="color: #006600;">method</span> = URLRequestMethod.<span style="color: #006600;">POST</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//now create an object with the variables you want to send as POST</span>
<span style="color: #000000; font-weight: bold;">var</span> postVariables:<span style="color: #0066CC;">Object</span> = <span style="color: #66cc66;">&#123;</span>variable1Name:variable1Data, variable21Name:variable2Data, variable3Name:variable3Data<span style="color: #66cc66;">&#125;</span>
urlRequest.<span style="color: #0066CC;">data</span> = UploadPostHelper.<span style="color: #006600;">getPostData</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'image.jpg'</span>, jpegBytes,<span style="color: #ff0000;">&quot;filedata&quot;</span>, postVariables<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//here is where the magic happens, filedata will be the name to retrieve the file</span>
urlRequest.<span style="color: #006600;">requestHeaders</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> URLRequestHeader<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'Cache-Control'</span>, <span style="color: #ff0000;">'no-cache'</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//from here it is just a normal URLLoader</span>
_pictureUploader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
_pictureUploader.<span style="color: #006600;">dataFormat</span> = URLLoaderDataFormat.<span style="color: #006600;">BINARY</span>;
_pictureUploader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">COMPLETE</span>, _onUploadComplete, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>;
_pictureUploader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span> urlRequest <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>How easy and beautiful is that?</p>
<p>So can get the code for the UploadPostHelper at the bottom of <a title="UploadPostHelper" href="http://marstonstudio.com/2007/10/19/how-to-take-a-snapshot-of-a-flash-movie-and-automatically-upload-the-jpg-to-a-server-in-three-easy-steps/" target="_blank">this post</a> or I have made a zip file with the <a title="UploadPostHelper" href="http://www.zedia.net/files/UploadPostHelper.zip" target="_blank">.as file here</a>.</p>
<p>Dude, 2007!</p>
<div id="facebook_like"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.zedia.net%2F2010%2Fsending-bytearray-image-and-variables-to-server-side-script-as-post-data-in-as3%2F&amp;layout=standard&amp;show-faces=false&amp;width=450&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:550px; height:70px;"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.zedia.net/2010/sending-bytearray-image-and-variables-to-server-side-script-as-post-data-in-as3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Loading a COLLADA model without loading a file</title>
		<link>http://www.zedia.net/2009/loading-a-colada-model-without-loading-a-file/</link>
		<comments>http://www.zedia.net/2009/loading-a-colada-model-without-loading-a-file/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 19:56:06 +0000</pubDate>
		<dc:creator>zedia.net</dc:creator>
				<category><![CDATA[Papervision 3D]]></category>
		<category><![CDATA[Augmented Reality]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Collada]]></category>
		<category><![CDATA[DAE]]></category>
		<category><![CDATA[Embed tag]]></category>
		<category><![CDATA[mimeType]]></category>
		<category><![CDATA[Papervision3D]]></category>

		<guid isPermaLink="false">http://www.zedia.net/?p=500</guid>
		<description><![CDATA[You know what is funny? It&#8217;s when every project your doing what was cool 2 years ago. Yeah I have been doing a lot of Papervision3D lately, but you know what, I&#8217;m fine with it, I actually like it. Anyway, I think Augmented Reality is retarded (ok not that much, but fun to say). So [...]]]></description>
			<content:encoded><![CDATA[<p>You know what is funny? It&#8217;s when every project your doing what was cool 2 years ago. Yeah I have been doing a lot of Papervision3D lately, but you know what, I&#8217;m fine with it, I actually like it. Anyway, I think Augmented Reality is retarded (ok not that much, but fun to say). So I&#8217;m doing all that 3D and usually it&#8217;s easier to just load COLLADA models, mostly because you get more complex shapes and the texturing is also easier, all done by the 3D artist. That also help with the workflow, 3D guy does his thing Flash guys does his and they can work at the same time, which is great.</p>
<p>Normally I would just load the DAE file externally, it&#8217;s just an xml file but some times it can get quite big. For the project I am currently working on, (stay posted this one is going to be great) I have a lot of very small models, like a lot (omg I sound like the 10 years old daughter I don&#8217;t have). So I can&#8217;t load all of them externally because this would make too many server call which would in return slow my application greatly. It is not well know (maybe Flex developer knew this more) but you can embed the DAE file inside your SWF by using the Flex embed tag, now available in Flash CS4. I found this info on the <a title="Embedding a collada file" href="http://pv3d.org/2008/12/06/embedding-a-collada-file/" target="_blank">pv3d.org</a> site. Here is the code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;../../../../../assets/domino.dae&quot;</span>, mimeType = <span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> daeAsset:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> byteArray:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> daeAsset<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray;
<span style="color: #000000; font-weight: bold;">var</span> _dae:DAE = <span style="color: #000000; font-weight: bold;">new</span> DAE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
_dae.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>byteArray<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Well that is pretty cool and it saves you some loading time and server calls. What I don&#8217;t like with the embed tag is that the path to your asset is relative to the class file path not to the project, so sometime it is very tedious to find the right path.</p>
<p>Now let say you were generating textures at runtime (you guessed right, that is what I am doing) you still need to put a listener on the DAE to know when the load is completed, because even though it&#8217;s faster than loading an external file it&#8217;s still not instantaneous. Here is the full code:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;../../../../../assets/domino.dae&quot;</span>, mimeType = <span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> daeAsset:<span style="color: #000000; font-weight: bold;">Class</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> byteArray:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> daeAsset<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ByteArray;
<span style="color: #000000; font-weight: bold;">var</span> _dae:DAE = <span style="color: #000000; font-weight: bold;">new</span> DAE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
_dae.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>FileLoadEvent.<span style="color: #006600;">LOAD_COMPLETE</span> , _daeLoadComplete, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
_dae.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>byteArray<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> _daeLoadComplete<span style="color: #66cc66;">&#40;</span>event:FileLoadEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
  event.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>FileLoadEvent.<span style="color: #006600;">LOAD_COMPLETE</span>, _daeLoadComplete<span style="color: #66cc66;">&#41;</span>;
  <span style="color: #000000; font-weight: bold;">var</span> child:DisplayObject3D = _dae.<span style="color: #006600;">getChildByName</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;modelName&quot;</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">//modelName is the name of the shape the was outputted from your 3D program, you can see that name in the trace from Papervision3D</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">var</span> bitmapMaterial:BitmapMaterial = <span style="color: #000000; font-weight: bold;">new</span> BitmapMaterial<span style="color: #66cc66;">&#40;</span>_texture, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//_texture is you runtime generated bitmapData</span>
  child.<span style="color: #006600;">material</span> = bitmapMaterial;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Ok, only 4 more articles to write to remove the annoying roar4milk widget from my homepage (the reason for the sudden music).</p>
<div id="facebook_like"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.zedia.net%2F2009%2Floading-a-colada-model-without-loading-a-file%2F&amp;layout=standard&amp;show-faces=false&amp;width=450&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:550px; height:70px;"></iframe></div>]]></content:encoded>
			<wfw:commentRss>http://www.zedia.net/2009/loading-a-colada-model-without-loading-a-file/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
