How to do a large file upload using Sinatra, haml, nginx, and passenger

upload

I need to be able to allow a user to upload 30-60 mb files at a time. Right now, I'm solving the problem with a simple form post:

%form{:action=>"/Upload",:method=>"post",:enctype=>"multipart/form-data"}
 - @theModelHash.each do |key,value|
   %br
   %input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value}
   =key
 %br    
%input{:type=>"file",:name=>"file"}
%input{:type=>"submit",:value=>"Upload"}

This form allows the user to select processing options contained in theModelHash and upload a file for processing. Problem is, this method both freezes the user's UI and also requires that the entire form be reposted when the user presses the 'back' button.

I've looked at SWFUpload, but have no idea how to integrate that into my relatively simple app. There's a page here about integrating it with Rails, but I'm using Sinatra, and am new enough to this whole web programming thing that I don't know how to modify those files to work with what I need to do.

Is there a how-to to add large file uploads to my form there? Something relatively simple that just adds in a progress bar and doesn't repost? I feel like I'm having to triple the size of my application just to make this feature play nice, and that's bothering me a bit.

Best Answer

The process is usually the following: (from four.livejournal.com/730831.html)

HTTP upload progress bars are rather obfuscated- they typically involve a process running on the server keeping track of the size of the tempfile that the HTTP server is writing to, then on the client side an AJAX call is made every couple seconds to the server during the upload to ask for the progress of the upload.

In order to do that, you need to install a module on your web server (be it apache(1), nginx(2) or lighttpd(3)). Which means that you can't run your Sinatra app as a standalone (ruby sinatraapp.rb), but that you have to run it through(4) one of the aforementioned webservers. The easiest way is to use Passenger(5).

For the client-side AJAX polling, try Ajax Upload(6).

HTH!

links

  • (1): piotrsarnacki.com/2008/06/18/upload-progress-bar-with-mod_passenger-and-apache/
  • (2): wiki.nginx.org/NginxHttpUploadProgressModule
  • (3): redmine.lighttpd.net/wiki/1/Docs:ModUploadProgress
  • (4): blog.zerosum.org/2008/7/4/passenger-3-sinatra
  • (5): modrails.com/
  • (6): valums.com/ajax-upload/