Sinatra: Managing File Uploads

While building the Movie Review app, I wanted users to be able to upload movie posters along with the movies they submitted, and in the end display the images. I found is a Ruby gem called paperclip but it only works with Rails, I also found another gem that allows you to use paperclip with rack-based apps. Finally I settled for the plain old Ruby File class, because all I wanted was to be able to write image files to disk.

Lets define the upload form.

<h1>Upload Image</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="image">
  <input type="submit" value="Upload">

Here we have a form that posts to an "upload" route in our app when submitted. The important thing to note is the "enctype" attribute on the form element. This is required when form submissions include files.

Next we define our route and handle the file submission.

post '/upload' do
  # Check if user uploaded a file
  if params[:image] && params[:image][:filename]
    filename = params[:image][:filename]
    file = params[:image][:tempfile]
    path = "./public/uploads/#{filename}"

    # Write file to disk, 'wb') do |f|

We check to see if a file was submitted, then proceed to save the file in an “uploads” folder in our public directory.

Using the File class you can also delete a file like this.

File.delete(path) if File.exist?(path)


Share this article.

Share Tweet Share
blog comments powered by Disqus