Aug 19, 2011

How can I do a large file upload using Sinatra, haml, nginx, and passenger?


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:

 - @theModelHash.each do |key,value|
   %input{:type=>"checkbox", :name=>"#{key}", :value=>1, :checked=>value}

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.


The process is usually the following: (from

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).



  • (1):
  • (2):
  • (3):
  • (4):
  • (5):
  • (6):

Related posts:

  1. How can I use curl to upload and post a file with the following form?
  2. Limit vsftpd upload to a given set of file-names
  3. What is a simple linux based file server with web front end for upload and download to user folders?
  4. MySQL error during a large upload of data
  5. Unable to upload file FTP

Leave a comment