Skip to content

manjula/rails-swfupload

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

h1. Multiple Uploads using Ruby on Rails, SWFUpload, and AttachmentFu

*by David South Jr*

Updated: Saturday, 7 June 2008, 2:37 PM


h2. SWFUpload

"SWFUpload":http://www.swfupload.org is a fantastic application which makes short work of a hard problem -- how to upload multiple files to a website. Most solutions using Ruby on Rails revolve around multiple fields and complex ajax calls to monitor the upload progress. These are all hard to implement because they lack compatibility across browsers, require difficult server set up, and usually fail to update quick enough to give real upload progress feedback to the user.

SWFUpload elegantly solves this problem by using a hidden Flash movie, clever Javascript callbacks, and CSS. It is cross-platform (Mac, Linux or Windows) and cross-browser (Safari, Firefox, IE) compatible.

For example, the process for a photograph website works something like this:

# The user clicks an "Upload photos" button.
# Instead of the standard browser "open file" dialog, a javascript call initiates the hidden flash movie's "open file" dialog.
# The user can shift- or control-click one or more files to upload.
# The flash movie receives the file list and initiates a javascript call.
# Javascript POSTs the first filename to the server, just like a standard HTML form would do.
# The server begins receiving the file.
# The flash movie watches the upload, sending periodic javascript calls which can be turned into a dynamic upload progress bar. Because it is a client-side progress indicator, it is accurate and updated frequently.
# The server reports a successful upload.
# The javascript runner pulls another filename from the flash queue and initiates the next upload.
# The process repeats until the last file is uploaded.

From the server's point of view, it's the same as a user uploading one file at a time -- greatly simplifying server set up.


h2. AttachmentFu

"Rick Olsen":http://techno-weenie.net/ programmed the best attachment plugin for Ruby on Rails -- "attachment_fu":http://svn.techno-weenie.net/projects/plugins/attachment_fu/. It manages the server side upload process, storage, and retrieval of almost any attachment. For photographs it manages resizing and thumbnails, too. Storage options include saving to the filesystem, saving attachments into the database, or saving onto the Amazon S3 storage network.

"Mike Clark":http://clarkware.com/cgi/blosxom of "The Pragmatic Studio":http://pragmaticstudio.com/ has written a great "tutorial for attachment_fu":http://clarkware.com/cgi/blosxom/2007/02/24.


h2. Put it all together

To combine SWFUpload and attachment_fu is to use the best of both worlds -- a powerful client (swfupload) and server (attachment_fu) upload process. After a lot of research, I put together a sample application that combines Ruby on Rails, AttachmentFu, and SWFUpload.

This application is based on the "PHP application demo":http://demo.swfupload.org/applicationdemo/index.php included in the "SWFUpload demos":http://demo.swfupload.org/.

This is a simple demonstration of how all these components can work together. Go through the code, pull it apart, read the "SWFUpload documentation":http://demo.swfupload.org/Documentation/. Then adapt it for your own use.


h2. Download

The public repository for rails-swfupload is on "github":http://github.com/davidsouth. To clone the repository:

@git clone git://github.com/davidsouth/rails-swfupload.git@


h2. Contact

If you have any suggestions, tips, bugs please email dave at "appeddesign.com":http://appeddesign.com.


h2. Requirements

# Ruby on Rails 2.1.0
# Freeimage
# Ruby gem image_science
# SQLite3 or MySQL

Included in this application:

# SWFUpload 2.1.0
# attachment_fu (7 Jun 2008)
# mimetype-fu (7 Jun 2008)


h2. Mimetype-fu

Flash sets the mimetype for each file to application/octet-stream. Attachment_fu requires the mimetype to be set correctly before it will accept the files. "Matt Aimonetti":http://railsontherun.com/ wrote "mimetype-fu":http://code.google.com/p/mimetype-fu/ to help attachment_fu properly set the mimetype.


h2. attachment_fu_hack

Using an "evil twin":http://errtheblog.com/posts/67-evil-twin-plugin plugin, I overrode several standard AttachmentFu methods.

# Uploads through SWFUpload will automatically detect mimetype.
# Transliterated uploaded filenames have extra underscores removed and the whole filename is downcased.
# When running tests a temporary directory is used rather than the public/photos directory


h2. Functional Test

I added two functional tests for the photos controller. It tests both create and swfupload. Since each time the test is run, it adds a file to the temporary directory, the same test will destroy the photo as well.


h2. Leopard Tips

To upgrade Mac OS X 10.5 (Leopard) to the latest version of Rails, first upgrade ruby gems (sudo gem update --system) then update Rails itself (sudo gem update). To install FreeImage and image_science, I suggest installing "Macports":http://macports.org and then using port to install FreeImage (port install freeimage). Use gems to install image_science (sudo gem install image_science).

Remember to install XCode tools (on Leopard install disk) before you can use Macports.

You can change the photo model to use mini_magick or RMagick in combination with ImageMagick. However, in my tests, I've found FreeImage/image_science to be much faster than ImageMagick/mini_magick.


h2. Links

Useful links and related information found while researching this project:

* "SWFUpload":http://swfupload.org/ - New home of the SWFUpload project.
* "Uploading Files With SWFUpload":http://blog.airbladesoftware.com/2007/8/8/uploading-files-with-swfupload - Excellent source of information.
* "Getting the _session_id from SWFUpload":http://blog.inquirylabs.com/2006/12/09/getting-the-_session_id-from-swfupload/ - The cookie plugin that comes with SWFUpload 2.0.1 may bypass the session_id problem. However, I have not had a chance to test it, yet.
* "Activeupload":http://developer.assaydepot.com/?p=6 - A non-attachment_fu solution to combine SWFUpload and Rails.
* "Caboose forum posts":http://beast.caboo.se/forums/2/posts?page=9 - Read the posts from Peter De Berdt.
* "Ruby forum discussion":http://www.ruby-forum.com/topic/114752 - More from Peter De Berdt.
* "Flornet's Blog":http://blog.flornet.fr/2007/05/29/swfupload-using-ruby-on-rails-demo/ - An SWFUpload 1.x Rails demo.
* "Working with attachment_fu":http://almosteffortless.com/2007/03/25/working-with-attachment_fu/ - Some attachment_fu tips.
* "Evil Twin Plugins":http://errtheblog.com/posts/67-evil-twin-plugin


h2. Todo

* Implement best method of passing session data through SWFUpload.

About

Photo upload demonstration using Ruby on Rails, SWFUpload and AttachmentFu

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published