0

I use Attachinray with Cloudinary, for direct upload. Everything works very well, except the fact that when I try to save files in my controller, I do not have the original filenames.

Here is a sample of my code:

class User
  has_attachments :pictures
end 

My form :

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

Once my files are uploaded, when I submit the form, I lost original filenames. Here is what I get for one file :

{"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"}

Does anyone know if it's possible to keep track of the original filename ?

1 Answers1

0

If you wish to have the original image's filename, you can use something like the following in your controller code:

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

If you need to store them in your DB, you'll probably need to permit it first and not access them directly.

Alternatively, you can tell Cloudinary to set the uploaded image's filename as the original filename:

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

Note that random characters are added in default. For more information: http://support.cloudinary.com/hc/en-us/articles/202520762-How-to-upload-images-while-keeping-their-original-filenames-

Itay Taragano
  • 1,901
  • 1
  • 11
  • 12
  • Thanks a lot ! I tried to pass use_filename as attachinary_file_field parameter, which was obviously not working. Now everything works perfectly ! – chloé roger Mar 27 '15 at 08:43