美文网首页
Sinatra 的隐藏技能

Sinatra 的隐藏技能

作者: Pope怯懦懦地 | 来源:发表于2019-02-26 23:15 被阅读4次

    至少对我来说是隐藏技能。

    文件上传

    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
    

    相关文章

      网友评论

          本文标题:Sinatra 的隐藏技能

          本文链接:https://www.haomeiwen.com/subject/dajtuqtx.html