Rails does not block on file uploads
Published over 6 years ago

I’ve been quite annoyed with the ignorance of people who keep telling every other person that Rails/Mongrel blocks on file upload.

Here are the steps to realize Rails does not block on file uploads :

  • Follow these steps :
null:Rails lifo$ rails fileuploads
null:Rails lifo$ cd fileuploads/
null:fileuploads lifo$ script/generate controller upload index

Make your RAILS_ROOT/app/views/upload/index.html.erb look something like below :

<% form_tag({}, { :multipart => true }) do %>
  <label for="file">File to Upload</label>
  <%= file_field_tag "file" %>
  <%= submit_tag %>
<% end %>

Now, generate a big fat file ( YMMV ) for testing purpose :

null:fileuploads lifo$ dd if=/dev/random of=out.txt bs=1000000 count=100
  1. Now start the server with script/server
  2. Open http://localhost:3000/upload in two tabs
  3. Start uploading that big fat file in first tab
  4. Switch the tab to second. And start pressing refresh button !!!

In all fairness, you may notice a momentary spike in memory/cpu usage while dealing with very large file uploads ( we’re talking a good 100s of MB here ) But believing that Rails process is blocked for the entire time it takes a user to upload the file from the browser, is outrageous.