Skip to content

Instantly share code, notes, and snippets.

@dchymko
Created March 28, 2017 04:58
Show Gist options
  • Save dchymko/4c1f50d23d9c7199af9e1a288bdb5ded to your computer and use it in GitHub Desktop.
Save dchymko/4c1f50d23d9c7199af9e1a288bdb5ded to your computer and use it in GitHub Desktop.
File upload in Sinatra for Lighthouse
get '/upload' do
erb (:upload)
end
post '/upload' do
if params[:file]
filename = params[:file][:filename]
file = params[:file][:tempfile]
file_dir = File.join(settings.public_folder, 'files')
File.open(File.join(file_dir, filename), 'wb') do |f|
f.write file.read
end
end
"file uploaded"
end
<html>
<body>
<h1> Search</h1>
<form action='/upload' enctype="multipart/form-data" method='POST'>
<input name="file" type="file" />
<input type="submit" value="Upload" />
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment