<?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>ADM Blog &#187; externalinterface</title> <atom:link href="http://blog.another-d-mention.ro/tag/externalinterface/feed/" rel="self" type="application/rss+xml" /><link>http://blog.another-d-mention.ro</link> <description>No matter how you see things, reality changes when you reach understanding</description> <lastBuildDate>Fri, 22 Jul 2011 06:56:49 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1</generator> <item><title>Open and Save files to Desktop without going to Server</title><link>http://blog.another-d-mention.ro/programming/java-script/open-and-save-files-to-desktop-without-going-to-server/</link> <comments>http://blog.another-d-mention.ro/programming/java-script/open-and-save-files-to-desktop-without-going-to-server/#comments</comments> <pubDate>Wed, 05 May 2010 13:21:01 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Java Script]]></category> <category><![CDATA[externalinterface]]></category> <category><![CDATA[filereference]]></category> <category><![CDATA[flash]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[open]]></category> <category><![CDATA[save]]></category> <guid
isPermaLink="false">http://blog.another-d-mention.ro/?p=817</guid> <description><![CDATA[Few days ago I was working on a jQuery based tool to QA the JavaScript functionality of a website and I was in need of a method to save and load my scripts to Desktop. In most cases this require a server script where you send the content and it will serve it to download [...]
Related posts:<ol><li><a
href='http://blog.another-d-mention.ro/programming/read-load-files-from-zip-in-javascript/' rel='bookmark' title='Permanent Link: Read/Load files from ZIP in JavaScript'>Read/Load files from ZIP in JavaScript</a></li><li><a
href='http://blog.another-d-mention.ro/programming/right-click-and-custom-context-menu-in-flash-flex/' rel='bookmark' title='Permanent Link: Right click and custom context menu in Flash/Flex'>Right click and custom context menu in Flash/Flex</a></li><li><a
href='http://blog.another-d-mention.ro/programming/use-any-font-with-cufon/' rel='bookmark' title='Permanent Link: Use any Font with Cufon'>Use any Font with Cufon</a></li></ol>]]></description> <content:encoded><![CDATA[<p>Few days ago I was working on a jQuery based tool to QA the JavaScript functionality of a website and I was in need of a method to save and load my scripts to Desktop.<br
/> In most cases this require a server script where you send the content and it will serve it to download and same for loading it. But a colleague suggested a better solution: to use the FileReference class.<br
/> First step was to create a simple flash with the required functionality and send/retrieve data with ExternalInterface. Failed miserably. Why ? Because it seems Flash will not allow you to open a File Dialog from script only. You literally have to click a button from the movie interface to be able to do that. If you knew that good for you, I didn't.</p><p>So, I've created another flash movie, where you pass the open and save image and callback and it will insert the flash with the specified images in place and look just like any other buttons.</p><p>Here's a small demonstration (other buttons beside open and save are just for show):<br
/> <iframe
src="http://blog.another-d-mention.ro/stuff/FileDialogs/index.html" with="400" height="170" frameborder="0" style="overflow:hidden"></iframe><br
/> So what do you need to set it up ?</p><p>First, you have to include the <strong>FileDialogs.js</strong> in your document (or just paste the code inside it into your own script).</p><p>Second, you need your JavaScript callbacks.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td
class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> openCallback<span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">=</span> input<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> saveCallback<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">return</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p><strong>openCallback()</strong> will be called once the user will select a file from the desktop and pass it's content as a parameter<br
/> <strong>saveCallback()</strong> will be called when the user clicks save, before the dialog to choose a file name opens, and it must return the content you want to save to disk</p><p>Third, you must setup the whole thing.</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td
class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// call this function on the onload event so the placeHolder element gets created</span>
<span style="color: #003366; font-weight: bold;">function</span> <span style="color: #000066;">onLoad</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Create a config object</span>
    <span style="color: #003366; font-weight: bold;">var</span> config <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FileDialogsConfig<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// width of the entire flash movie (buttons width + padding)</span>
    config.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">45</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// height of the movie</span>
    config.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">16</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// padding between buttons</span>
    config.<span style="color: #660066;">padding</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// open dialog button image and javascript callback function</span>
    config.<span style="color: #000066;">open</span>.<span style="color: #660066;">image</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;open.png&quot;</span><span style="color: #339933;">;</span>
    config.<span style="color: #000066;">open</span>.<span style="color: #660066;">handler</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;openCallback&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// save dialog button image and javascript callback function</span>
    config.<span style="color: #660066;">save</span>.<span style="color: #660066;">image</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;save.png&quot;</span><span style="color: #339933;">;</span>
    config.<span style="color: #660066;">save</span>.<span style="color: #660066;">handler</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;saveCallback&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// create the movie inside an element with the given id and configuration</span>
    <span style="color: #003366; font-weight: bold;">new</span> FileDialogs<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;placeHolder&quot;</span><span style="color: #339933;">,</span> config<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p><strong>Note:</strong> Handler callbacks in the config must be declared as String and not reference. IE will freak out and I don't want to fix it.</p><p>You can have only one button (open or save) if you chose so. Just don't set the other one.</p><p>That's about it. The placeHolder can be any container element. The script will set it's style size based on the configuration, the movie will have transparent background so don't worry about that, and it will set it's float property to left so you can add other stuff after it. But all those things are easy to configure if you mess a bit with the FileDialogs.js file.</p><p>Cheers!</p><p><a
href="http://cache.another-d-mention.ro/stuff/FileDialogs/FileDialogs.zip">Download Source and Demo</a></p><p>Related posts:<ol><li><a
href='http://blog.another-d-mention.ro/programming/read-load-files-from-zip-in-javascript/' rel='bookmark' title='Permanent Link: Read/Load files from ZIP in JavaScript'>Read/Load files from ZIP in JavaScript</a></li><li><a
href='http://blog.another-d-mention.ro/programming/right-click-and-custom-context-menu-in-flash-flex/' rel='bookmark' title='Permanent Link: Right click and custom context menu in Flash/Flex'>Right click and custom context menu in Flash/Flex</a></li><li><a
href='http://blog.another-d-mention.ro/programming/use-any-font-with-cufon/' rel='bookmark' title='Permanent Link: Use any Font with Cufon'>Use any Font with Cufon</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://blog.another-d-mention.ro/programming/java-script/open-and-save-files-to-desktop-without-going-to-server/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> </channel> </rss>
