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

Attachinary : can I keep the original filename?

我使用 Attakinray 和 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"}

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

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

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-