Thursday, April 26, 2007

File upload in Flex + Ruby on Rails

Flex provides an API to upload and download a file. It uses the flash.net.FileReference class. Find the below code

Actionscript 3.0:

Call the "uploadFiles()" method to start uploading a file. Change the URLString variable to point to your controlle

// ActionScript file
import flash.net.FileReference;
import flash.net.URLRequestHeader;

private var uploadFileRefList:FileReference = new FileReference();
private var uploadURLReq:URLRequest;
private var URLString:String="upload/attachment";

private function uploadFiles():void
{
uploadFileRefList.addEventListener(Event.SELECT, uploadSelectHandler);
try {
var success:Boolean = uploadFileRefList.browse();
} catch (error:Error) {
trace("Unable to browse for files. " + error);
}
}
private function doUpload(file:FileReference):void
{
uploadURLReq = new URLRequest(URLString);
file.addEventListener(Event.COMPLETE, uploadCompleteHandler);
file.addEventListener(IOErrorEvent.IO_ERROR, uploadIoErrorHandler);
file.addEventListener(ProgressEvent.PROGRESS, uploadProgressHandler);
try
{
file.upload(uploadURLReq);
} catch (error:Error) {
trace("Unable to upload file. " + error);
}
}
private function uploadSelectHandler(event:Event):void
{
doUpload(uploadFileRefList);
}
private function uploadProgressHandler (event:ProgressEvent):void
{
upload_btn.enabled=false;
trace(Math.floor((event.bytesLoaded/event.bytesTotal)*100) + "% complete");
}
private function uploadIoErrorHandler (event:Event):void
{
trace("Upload failed: " + event);
}
private function uploadCompleteHandler (event:Event):void
{
upload_btn.enabled=true;
trace("Upload successfull: ");
}

Ruby on Rails:

Create a controller "upload" and place the below code
   def attachment
render(:xml => "") if saveFileAttachment(params[:Filedata],params[:Filename].to_s)
end

Then create a helper method "saveFileAttachment"

def saveFileAttachment(pFile,pFileName)
vFilePath = "public/"+pFileName
return false unless File.open(vFilePath, "wb") { |vBuffer| vBuffer.write(pFile.read) }
return true
end

10 comments:

Arul said...

All the best for your blog :)

Fix the typo in the title ;)

Regards,
Arul

thinkpixels said...

Thanks man...fixed

Romain said...

I think your saveAttachmnet method is missing the filename. Also the method name should lowerCamelCase Should most likely be:

saveFileAttachment(pFile,pFileName)
vFilePath = "public/"
return false unless File.open(vFilePath+pFileName, "wb") { |vBuffer| vBuffer.write(pFile.read) }
return true

thinkpixels said...

Thanks romain..you are right..I missed out the file name in the save method.Thanks for pointing out.

Indian Handicrafts said...

thanks for giving informaion about google API for Flex
www.posintechnologies.com
www.posintech.com
http://flex.posintech.com

Anonymous said...

If you need authentication and uploads (since FileReference doesnt send header info, cookies, etc) consider Merb http://merb.devjavu.com/attachment/ticket/82/fixation_exclusion_white_list.diff

Anonymous said...

How to upload video using Flex and PHP anyone knowz proper steps plz help out on shinde.amruta@gmail.com

thinkpixels said...

@amruta - Why dont you use the same code to upload the video file

Anonymous said...

I am getting this error whenever I try to upload it from my local machine. have any idea why this error comes


Upload failed: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2038: File I/O Error. URL: http://localhost:3000/users/upload_image/"]

Anonymous said...

its good example but could u give example to use ffmpeg for upload video from flexand store it in rails
with snapshots
abhishekchess1@gmail.com