如何下载文件并获取其名称?

时间:2015-07-19 11:33:33

标签: ruby-on-rails ruby-on-rails-4

请帮助解决问题

使用表格我上传文件:

<%= form_for(@video) do |f| %>
  <%= f.text_field :title %>
  <%= f.file_field :video %>

  <%= f.submit %>
<% end %>

控制器:

  def create
    p '============'
    p params[:video][:title]
    p params[:video][:video]
    p params[:video][:original_filename]
    p params.inspect

    redirect_to root_url
  end

  def video_params
    params.require(:video).permit(:title, :video, :original_filename)
  end

我需要带到控制台:

p params[:video][:original_filename]

但是控制台没有出现original_filename:

Started POST "/videos" for 127.0.0.1 at 2015-07-19 14:27:19 +0300
Processing by VideosController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"FZASvIyDWQvwqP/P8DewOdViRooE01cEGs8JsjZKweagcvfCiL+wfKCyAfYaW0Id8y/KkX1oAMYpP3uWCM0n9A==", "video"=>{"title"=>"ррррр", "video"=>#<ActionDispatch::Http::UploadedFile:0x007f04d821d318 @tempfile=#<Tempfile:/tmp/RackMultipart20150719-29849-pkn4g3.mp4>, @original_filename="mmm.mp4", @content_type="video/mp4", @headers="Content-Disposition: form-data; name=\"video[video]\"; filename=\"mmm.mp4\"\r\nContent-Type: video/mp4\r\n">}, "commit"=>"Create Video"}
"============"
"ррррр"
#<ActionDispatch::Http::UploadedFile:0x007f04d821d318 @tempfile=#<Tempfile:/tmp/RackMultipart20150719-29849-pkn4g3.mp4>, @original_filename="mmm.mp4", @content_type="video/mp4", @headers="Content-Disposition: form-data; name=\"video[video]\"; filename=\"mmm.mp4\"\r\nContent-Type: video/mp4\r\n">
nil

1 个答案:

答案 0 :(得分:1)

请试试这个

 p params[:video][:video].original_filename