Posted by mrconnerton on May 20, 2009 at 4:58pm
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:
<?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.
Matthew Connerton
http://www.mrconnerton.com

Comments
It's not working...
Hello.
Thank you for your post. I tried to implement this solution but it's not working. I created a new discussion for this in here as this one is quite old and my topic is not exactly the same:
http://groups.drupal.org/node/71148