Upload and download files in Sinatra
A simple example.
List
get '/' do
list = Dir.glob("./files/*.*").map{|f| f.split('/').last}
# render list here
end
Upload
post '/' do
tempfile = params['file'][:tempfile]
filename = params['file'][:filename]
File.copy(tempfile.path, "./files/#{filename}")
redirect '/'
end
Download
get '/download/:filename' do |filename|
send_file "./files/#{filename}", :filename => filename, :type => 'Application/octet-stream'
end
Delete
get '/remove/:filename' do |filename|
File.delete("./files/#{filename}")
redirect '/'
end
Written by Bohdan Schepansky
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#