附件:我可以保留原始文件名吗?

时间:2015-03-24 14:57:14

标签: ruby-on-rails image-upload cloudinary

我使用Attachinray和Cloudinary进行直接上传。一切都很好,除了当我尝试在控制器中保存文件时,我没有原始文件名。

以下是我的代码示例:

class User
  has_attachments :pictures
end 

我的表格:

= simple_form_for @user do |f|
  = f.attachinary_file_field :pictures, as: :attachinary
  = f.submit t('.submit')

上传文件后,当我提交表单时,我丢失了原始文件名。这是我得到的一个文件:

{"public_id"=>"ryfeummp2ikmzkss4cfy", "version"=>1427873505, "signature"=>"8475fccf9914dd05f6fg622ee39c1cb7ddd25f11", "width"=>78, "height"=>100, "format"=>"png", "resource_type"=>"image", "created_at"=>"2015-03-24T14:48:25Z", "tags"=>["development_env", "attachinary_tmp"], "bytes"=>19882, "type"=>"upload", "etag"=>"1fefdabba402263d18a92238ba4275c9", "url"=>"http://res.cloudinary.com/dd9blzv7x/image/upload/v1427873505/ryfeummp2ikmzkss4cfy.png", "secure_url"=>"https://res.cloudinary.com/dd9blzv7x/image/upload/v1427873505/ryfeummp2ikmzkss4cfy.png"}

是否有人知道是否可以跟踪原始文件名?

1 个答案:

答案 0 :(得分:0)

如果您希望获得原始图像的文件名,可以在控制器代码中使用以下内容:

params[:user][:pictures].each do |picture| 
  # Do whatever you need with picture.original_filename
end

如果您需要将它们存储在数据库中,您可能需要首先permit,而不是直接访问它们。

或者,您可以告诉Cloudinary将上传的图像文件名设置为原始文件名:

f.attachinary_file_field :pictures, as: :attachinary, cloudinary: {use_filename: true}

请注意,默认情况下会添加随机字符。欲获得更多信息: http://support.cloudinary.com/hc/en-us/articles/202520762-How-to-upload-images-while-keeping-their-original-filenames-