A Screencast Explaining and Demoing File Promises

File promises are kind of a difficult concept to describe, so I decided to explain them using a video. Hopefully this clarifies what file promises are, and why it’s such a cool new feature of AIR 2.0:

Here’s the code that creates a URLFilePromise for every file to be downloaded and puts them on the clipboard. But first, some code context:

  • onDragStart is the event handler that gets called when the user starts dragging items from the list of objects.
  • bucketList is the ComboBox of bucket names.
  • objectList is the DataGrid of objects.
  • The function s3.getTemporaryObjectURL(...) generates a temporary public URL that points to the specified object. It’s the URL that the URLFilePromise will use to access the file.

private function onDragStart(e:DragEvent):void{if (bucketList.selectedIndex == 0 || objectList.selectedItems == null) return;var c:Clipboard = new Clipboard();var items:Array = objectList.selectedItems;this.filePromises = new Array();for each (var item:Object in items){var fp:URLFilePromise = new URLFilePromise();var req:URLRequest = new URLRequest(s3.getTemporaryObjectURL(bucketList.selectedItem.name, item.key, 60));fp.request = req;fp.relativePath = item.key;this.filePromises.push(fp);}c.setData(ClipboardFormats.FILE_PROMISE_LIST_FORMAT, this.filePromises);NativeDragManager.doDrag(objectList, c, null, null, null);}

Below is the function that gets called when the drop is complete. It’s responsible for opening the ProgressWindow component and passing it the array of URLFilePromise objects which the ProgressWindow component hooks into in order to receive progress events.

private function onDragComplete(e:DragEvent):void{var progressWindow:ProgressWindow = new ProgressWindow();progressWindow.open(false);progressWindow.setFilePromises(this.filePromises);}

The code for S3E is open source and available on Google Code.