Created
June 6, 2014 02:00
-
-
Save pobing/3bbbb1aec3a033a8c2e3 to your computer and use it in GitHub Desktop.
sinatra download file demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
# 2 download | |
get '/some/:file' do |file| | |
file = File.join('/some/path', file) | |
send_file(file, :disposition => 'attachment', :filename => File.basename(file)) | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment