Flex: Save binary bytearray data from actionscript 3 to drupal with amfphp

Submitted by matthew on May 20, 2009 - 1:00pm

I have been looking for a way to upload a file in flex and save the bytearray directly to drupal via amf. This was written on Drupal 5 however it can easily be added to Drupal 6. Just note there is already a file_services module in D6 and not d5. Here's what I have come up with that works great:

In actionscript 3:


// Use file referance to select a file on the users computer and then load it

public function startUpload():void {
_rf = new FileReference();
_rf = listFiles.selectedItem.file;
_rf.addEventListener(Event.COMPLETE, onLoadComplete);
_rf.load();
}
// On load you can grab the binary data / bytearray from the FileReference.data property
// Make sure you compress the data before you send it.
// Then we call the file.save method with the bytearray and the name of the file

public function onLoadComplete(event:Event):void {
var ba:ByteArray = _rf.data;
ba.compress();
file.save(ba,_rf.name);
}

// Once the file is saved we get returned the file name!
public function onFileSave(event:ResultEvent):void {
var filename:String = event.result;
}

In PHP:

/**
* Implementation of hook_service()
*/
function file_service_service() {
return array(
array(
'#method' => 'file.save',
'#callback' => 'file_service_save',
'#access callback' => 'file_service_save_access',
'#args' => array(
array(
'#name' => 'data',
'#type' => 'struct',
'#description' => t('Save bytearray data to a file'),
),
),
'#return' => 'string',
'#help' => t('Returns the filename saved.')
),
);
}

// This is all pulled from the drupal file_save_data() function with one minor change
function file_service_save($bytearray, $dest, $replace = FILE_EXISTS_RENAME) {
$file = tempnam(realpath(file_directory_temp()), 'file');

if (!$fp = fopen($file, 'wb')) {
drupal_set_message(t('The file could not be created.'), 'error');
return 0;
}
// Here is the magic, we just need to add gzuncompress to the bytearray data
$data = gzuncompress($bytearray->data);
fwrite($fp, $data);
fclose($fp);

if (!file_move($file, $dest, $replace)) {
return 0;
}

return $file;
}

function file_service_save_access() {
return TRUE;
}

Hope this is quite useful for you guys. Note that this will allow any binary file to be saved to the server so you should have some safety precautions to filter harmful extensions and rename them.

What is Drupal?

Drupal - a completely free, open source content management system that many organizations are using today to provide for their needs. Drupal's many features and add-ons allow it to be used for a variety of website needs. Drupal is a good choice for today's organizations of any size because it is free, easy, and incredibly flexible.

Copyright © mrconnerton.com, 2008-2010. All Rights Reserved Drupal theme by Kiwi Themes.