至少对我来说是隐藏技能。
文件上传
get "/upload" do
<<-HTML
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="images[]" multiple />
<input type="submit" />
</form>
HTML
end
# Handle POST-request (Receive and save the uploaded file)
post "/upload" do
pp params
params['images'].each do |file|
File.open("./upload/#{file[:filename]}", "w") do |f|
f << File.open(file[:tempfile], "r").read
end
end
res = "I received the following files:\n"
res << params['images'].map{|f| f[:filename] }.join("\n")
res
end
文件下载
get '/list' do
list = Dir.glob("./upload/*.*").map{|f| f.split('/').last}
# render list here
end
get '/download/:filename' do |filename|
send_file "./upload/#{filename}"
end
网友评论