File downloading with Adobe AIR using HTML and jQuery
comments (16) Views: 13,457
- Open the HTML file for your project. If you're importing the source from this article, the file is named DownloadFile.html
Add the following lines of code to the head of your document.
- The first few lines add some basic styling to the application. Nothing fancy, just enough to keep stuff away from the edges.
- The fifth and sixth lines include our AIR specific code: the AIRAliases file (contains shortcuts we'll use later), and the AIRIntrospector, essentially Firebug for AIR applications.
- Line 7 includes jQuery, adjust the filename to the version you're using in your project.
- Finally, line 8 includes Application.js. The file name is nothing special, but it helps to indicate that this file contains your main code for the application.
Add the following lines of code to the body of your document
- Here we're simply adding some standard form controls, along with their associated labels. One for the file URL, one to allow downlocation selection, and one for the submit button.
- That's all there is to the interface. Go ahead and run the application and see what you get.
- If your application is behaving unexpectedly, be patient. We'll get to that in the next section.
In this project we're going to give you, the user, the chance to select where you want the file to end up. One of the excellent things about AIR is that the same AIR file can be installed on Windows, Mac, and Linux. Adobe has given us several built in properties of the File object which correspond to specfic locations on your machine; even though the locations might not exist in your operating system. These properties are applicationDirectory: where the app itself gets installed; applicationStorageDirectory: where custom data for the app is stored; and desktopDirectory: the user's desktop.
- We've written the HTML code for our interface, now let's give this app it's functionality. Let's create a file in our /lib/ directory, we'll call it Application.js. If the file already exists, simply open it.
- In that file place the following code. This helps keep our simple application organized. Larger, more complex applications will benefit from a more regimented structure, but for this example, everything in one file is fine.
The first thing we need to do is to add a binding to the form element in the HTML page. We want to intercept it's submision and do something with the values that it will contain. Add the following lines of code directly under the line reading /* INTERFACE BINDINGS */ and let's review 3 key points.
- Line 1 is simply a reference to the form itself. We'll be reusing that, so it makes sense to save it off. It has a dollar sign in front of it to indicate that the variable contains a jQuery object.
- Line 5: If you've never seen preventDefault being used, or you're just used to issuing a "return false" command to stop form submissions or click events, then you should read this article from Douglas Neiner about the practice. jQuery's anonymous function allows us to pass "e", or the event that we just triggered, through to the body of the function so that we can take advantage of built-in methods of the event object.
- Line 15: The downloadFile function will do just what you think it should do. We pass the remote URL, along with which download location the user selected: desktop, or storage, directory.
Finally, let's add our downloadFile function, the workhorse of this demo application, and walk through the code. Paste, or type, the following code directly below the line reading /* FUNCTION DEFINITIONS */.
- Line 3: The first thing we're doing is grabbing the filename from the remote URL. This is optional...you can use the filename as it exists remotely, or you can create a new filename client side. It's up to you.
- Lines 5-6: This is where the user's choice of download location comes into play. We determine where they want to store the file, then use the built in method of the File object, resolvePath, to create an OS specific file path.
- Line 7: Creates new URLStream, which allows you to pull down the bits and bytes of the remote URL to the file system.
- Line 8: Creates an event listener which fires when the bits and bytes are available on the local machine. Note that they're not technically "saved", until the callback function fires.
- Lines 9: When it comes down to it, all files are made up of bytes. Here we create a new ByteArray, essentially an empty vessel, to receive the contents of the remote file.
- Line 10: Does the pouring of the bytes into the empty ByteArray container.
- Lines 11-14: Just like we had to create a URLStream to load a remote URL, we create a new FileStream to access a local URL. Then we open it, save the contents of the ByteArray, and close the file
- It's worth noting that this specific implementation is looking for a specific file to download. AIR has to know what sort of file you're expecting so that it can give it the correct file name and extension.
Hope you enjoyed this article and don't forget to download the source, including the actual AIR file.
If this article was interesting, or helpful, or even wrong, please consider leaving a comment, or buying something from my wishlist. It's appreciated!