Skip to content

Instantly share code, notes, and snippets.

@runemadsen
Created October 17, 2012 13:45
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save runemadsen/3905593 to your computer and use it in GitHub Desktop.
Save runemadsen/3905593 to your computer and use it in GitHub Desktop.
Sinatra File Upload
require 'sinatra'
get "/" do
erb :form
end
post '/save_image' do
@filename = params[:file][:filename]
file = params[:file][:tempfile]
File.open("./public/#{@filename}", 'wb') do |f|
f.write(file.read)
end
erb :show_image
end
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<h1>Upload Image</h1>
<form action="http://itp.nyu.edu/~rsm347/sinatra/file_upload/save_image" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload image">
</form>
</body>
</html>
<html>
<head>
<title>Show Image</title>
</head>
<body>
<h1>See Image</h1>
<img src="http://itp.nyu.edu/~rsm347/sinatra/file_upload/public/<%= @filename %>" />
</body>
</html>
@rmetzler
Copy link

rmetzler commented May 6, 2015

If you reuse this script, be aware of the hardcoded URLs in the ERB-files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment