Rails 3 - upload files to public directory

前端 未结 3 1376
你的背包
你的背包 2020-12-02 10:34

I\'m looking for a simple way to upload a file (an xml file) to the public directory of Rails 3. Once it\'s there I want to open it, parse the contents and delete the file a

3条回答
  •  执笔经年
    2020-12-02 11:14

    a. Form

    <%= form_for :file_upload, :html => {:multipart => true} do |f| %>
      <%= f.file_field :my_file %>
      <%= f.submit "Upload" %>
    <% end %>
    

    b. controller

    def file_upload  
      require 'fileutils'
      tmp = params[:file_upload][:my_file].tempfile
      file = File.join("public", params[:file_upload][:my_file].original_filename)
      FileUtils.cp tmp.path, file
      ... # YOUR PARSING JOB
      FileUtils.rm file
    end
    

    But you can parse just tempfile, so you don't need to copy it to public dir and it will automatically deleted

提交回复
热议问题