Skip to content

Instantly share code, notes, and snippets.

@thinkerbot
Created February 23, 2010 18:16
Show Gist options
  • Save thinkerbot/312512 to your computer and use it in GitHub Desktop.
Save thinkerbot/312512 to your computer and use it in GitHub Desktop.
A file upload example
require 'rubygems'
require 'sinatra'
get '/' do
erb :form, :locals => {:file => nil}
end
post '/' do
erb :form, :locals => {:file => request['file']}
end
template :form do
%q{
<html>
<body>
<form action="/" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit" value="Upload">
</form><% if file %>
<dl>
<dt>Input Name :name</dt>
<dd><%= escape_html file[:name] %></dd>
<dt>Content-Type :type</dt>
<dd><%= escape_html file[:type] %></dd>
<dt>Filename :filename</dt>
<dd><%= escape_html file[:filename] %></dd>
<dt>Head :head</dt>
<dd><pre><%= escape_html file[:head] %></pre></dd>
<dt>Content :tempfile</dt>
<dd><pre><%= escape_html file[:tempfile].read %></pre></dd>
</dd><% end %>
</body>
</html>
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment