Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Adobe AIR Developer Center /

Using drag-and-drop support of remote files in Adobe AIR 2

by Christian Cantrell

Christian Cantrell
  • blogs.adobe.com/cantrell

Created

17 November 2009

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScript Adobe AIR desktop file management Flex

Requirements

Prerequisite knowledge

This article is intended for developers who are comfortable with ActionScript 3. Some experience with Adobe AIR is recommended.

User level

Intermediate

Required products

  • Adobe AIR

One of the biggest advantages of building an Adobe AIR application rather than (or in addition to) a browser-based application is the availability of desktop APIs. For example, AIR applications can access the file system, create native windows for desktop notifications, and have full access to the system clipboard for copy-and-paste as well as drag-and-drop operations. But as more and more data is being moved online, it's increasingly common for files to be stored remotely rather than on your local machine. AIR has always provided excellent networking APIs for accessing remote data, but until the introduction of file promises in AIR 2, it has never been possible to drag remote files out of an AIR application.

A file promise is what you put on the clipboard when you want the user to be able to drag and drop files that do not yet exist, or that exist elsewhere (not locally on your machine). For example, files that are on a remote server, or files that you only want to generate (such as a CSV file) when the user tries to drag and drop them. A file promise isn't an actual file; rather, as its name suggests, it represents the promise to deliver a file at some point in the future, after the user has selected a drop location.

This article covers the following topics:

  • Using the system clipboard in Adobe AIR
  • Writing your own file promise implementation
  • Using the URLFilePromise
  • S3E: an example of file promises in a real-world context

About the clipboard

Adobe AIR gives developers full access to both reading and writing from the system clipboard. In general, the clipboard is used for placing data for other applications to access; you can also create your own clipboard instances to allow dragging and dropping across applications. The following snippet shows a simple example of putting a string on the general clipboard so that it can be pasted into another application:

import flash.desktop.ClipboardFormats; var toCopy:String = "Hello, Adobe AIR."; Clipboard.generalClipboard.clear(); Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, toCopy);

Here's a simple example of placing some text on a clipboard for dragging out of your AIR application:

var toDrag:String = "Hello, Adobe AIR."; var cb:Clipboard = new Clipboard(); cb.setData(ClipboardFormats.TEXT_FORMAT, toDrag); NativeDragManager.doDrag(someComponent, cb);

The following list shows all the data formats that the Adobe AIR clipboard supports (as defined in the ClipboardFormats class):

  • BITMAP_FORMAT
  • FILE_LIST_FORMAT
  • FILE_PROMISE_LIST_FORMAT
  • HTML_FORMAT
  • TEXT_FORMAT
  • RICH_TEXT_FORMAT
  • URL_FORMAT

Note: Data placed on the clipboard is available to any other application that wants to access it, not just other Adobe AIR applications.

To put files on the clipboard that you currently have a reference to, you use the FILE_LIST_FORMAT clipboard format and pass in an array of File objects. To put files on the clipboard that you do not yet have—a reference to (for example, files that exist on a remote server)—you use the FILE_PROMISE_LIST_FORMAT clipboard format and pass in an array of objects that implement IFilePromise.

Writing your own file promise implementation

Since a file promise is a promise to get a file at some point in the future, the AIR runtime needs to know how to get that file. It might be that the file exists on a remote server, or that it simply hasn't been created yet. The IFilePromise interface is the contract between the runtime and the developer which describes how a file that doesn't exist yet will exist when the operating system asks for it.

An IFilePromise implementation must provide the following three methods:

  • open: Must return a reference to an object that implements IDataInput.
  • close: Called when the runtime is done reading from the IDataInput.
  • reportError: Called in the event that the runtime experiences an error while reading from the IDataInput object.

An IFilePromise must also define two properties:

  • isAsync: Specifies whether or not the data provided by the IDataInput will be provided synchronously or asynchronously. If it will be provided asynchronously (which will probably be the case most of the time), your IDataInput must also implement IEventDispatcher so that progress events can be thrown. If isAsync returns false, it is expected that the data will be available immediately.
  • relativePath: The path of the file that will be created relative to the drop location. For example, if relativePath is NewMusic/foo.mp3 and the drop operation occurs on the desktop, the final destination of the file on Mac OS X or Linux will be ~/Desktop/NewMusic/foo.mp3.

Having the option of implementing your own IFilePromise means that you have a great deal of flexibility in how your file gets created. You might generate it on demand, retrieve it from an FTP server, or you might download it over HTTP. But we figured that in most cases, developers will want to download file promises from a remote server, so we went ahead and created an implementation of IFilePromise called URLFilePromise so that you wouldn't have to.

Using URLFilePromise

The URLFilePromise implementation of IFilePromise makes putting files that are located on a remote HTTP server extremely simple. For example, if you had a DataGrid called fileData full of information about files on a remote server, the following code is all you would need to enable your end user to drag and drop those files out of your application:

var cb:Clipboard = new Clipboard(); var items:Array = fileData.selectedItems; var promises:Array = new Array(); for each (var item:Object in items) { var fp:URLFilePromise = new URLFilePromise(); var req:URLRequest = new URLRequest(item.url); fp.request = req; fp.relativePath = item.name; promises.push(fp); } cb.setData(ClipboardFormats.FILE_PROMISE_LIST_FORMAT, promises); NativeDragManager.doDrag(fileData, cb);

When your end user drags one or more files out of your data grid and drops them anywhere on the file system, all the selected files will be downloaded in the background as quickly as possible (through multiple network connections, if possible) and will appear as they arrive.

If you want to asynchronously monitor the progress of the file downloads, be sure to register for the PROGRESS event. Additionally, you may want to register for the COMPLETE and HTTPStatusEvents so that your application can stay informed of the state of the downloads.

Some things to remember

Below are a few things to keep in mind when using file promises:

  • File promises only work with drag-and-drop operations (not with copy and paste). You cannot put file promises on the general clipboard for end users to paste. (Actually, you can, but setData will return false indicating that it won't work.)
  • File promises only work between an AIR application and the native file system. You can't use file promises to drag and drop or copy and paste between AIR or native applications.
  • If a file promise fails for any reason, the partial file is automatically removed from the file system so your code does not have to worry about cleaning it up. Previously downloaded files will not be deleted, and subsequent downloads will still be attempted.

S3E: File promises in action

S3E is an AIR application that I wrote for managing files stored on the Amazon S3 service.

In my original version, to get files into the application (and transferred over to S3), you could use copy-and-paste or drag-and-drop support. To get files out, however, you could only choose to copy them, which downloaded them in the background and then placed them on the clipboard. There was no way to drag a representation of a remote file out of the application and drop it on the local file system to be downloaded to the chosen location.Thanks to file promises, the newest version of S3E now supports dragging and dropping of multiple files in both directions. You can install S3E from the AIR Developer Center, or download the source code from Google Code.

Where to go from here

This article covers the basics of drag-and-drop, described writing your own IFilePromise, and demonstrated the use of the URLFilePromise. For more information on file promises, see:

  • Screencast explaining and demoing file promises
  • S3E on the AIR Developer Center
  • S3E on Google Code

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License

More Like This

  • Using the Microphone capabilities in Adobe AIR 2
  • Using the SQLite database access API in Adobe AIR
  • Writing multiscreen AIR apps
  • Creating a socket server in Adobe AIR 2
  • User experience considerations with SQLite operations
  • Exploring the new file capabilities in Adobe AIR 2
  • Reducing CPU usage in Adobe AIR
  • Building multilingual Flex applications on Adobe AIR
  • Resolving DNS records in Adobe AIR 2
  • Retrieving a list of network interfaces in Adobe AIR 2

Tutorials & Samples

Tutorials

  • Retrieving a list of network interfaces in Adobe AIR 2
  • Writing multiscreen AIR apps
  • Using the AIR 2 NativeProcess API to create a screen recorder
  • Resolving DNS records in Adobe AIR 2

Samples

  • Using the Salesbuilder Adobe AIR sample application

Adobe AIR Forums

More
04/11/2012 Surround sound 5.1 with Air 3.2 on desktop app?
12/12/2011 Live Streaming H.264 Video on iOS using AIR
04/17/2012 HTMLLoader - Google Maps?
04/12/2012 Tabindex in forms on mobile?

Adobe AIR Blog

More
07/09/2012 Protected: Publishing Adobe AIR 3.0 for TV on Reference Devices
07/08/2012 Source Code: Adobe AIR 3.3 Retina Video Application
07/06/2012 Application specific File Storage on Adobe AIR based ios Application
07/04/2012 Recent Work - iPad/Android App: Inside My toyota

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement