2

Let's consider such case:

  • I have an image which contains, for example, a guitar
  • Its name is guitar.png
  • After uploaded it changes name to random string like kouyyvrmbubwkrh7ayvk.png
  • Now i want to have a link to this file, but clicking it should download the file with changed name, eg. "my-custom-attachment.png", or something

Now I'm using simple <a>...</a> with angular attribute:

<a ng-href="{{ vm.resource.attachment_url }}"> Download attachment </a>

Where vm.resource.attachment_url is obviously fetched object with internal API

I wasn't able to find such implementation in Cloudinary API. Also, I hope I'm clear enough.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
blisher
  • 75
  • 9
  • You can refer http://stackoverflow.com/questions/5535981/difference-between-rails-send-data-and-send-file-with-example. You can use `send_file` to rename the file accordingly. – Amit Badheka Jan 25 '16 at 13:13

2 Answers2

2

Cloudinary supports delivering images as attachments forcing it to download and store with its original filename.
This is done by simply adding the fl_attachment to the URL, or setting the flags parameter to attachment when using the helper methods, e.g.:
cloudinary.url('kouyyvrmbubwkrh7ayvk.png', {flags: 'attachment'}); In your specific case this will result with the image being saved as guitar.png.

More information: http://support.cloudinary.com/hc/en-us/articles/202521252-How-can-I-deliver-an-image-as-an-attachment-

Nadav Ofir
  • 778
  • 4
  • 6
0

Use the download attribute of the anchor, which specifies what name will receive the downloaded file:

<a ng-href="{{ vm.resource.attachment_url }}" download="my-custom-attachment.png"> Download attachment </a>

See the demo how the attribute works.

Another option is to download the file content into a Blob object, and then throw the blob content with a modified filename. Check the demo.

Dmitri Pavlutin
  • 18,122
  • 8
  • 37
  • 41
  • Looks nice but it works only for local files. I need to fetch files from cloudinary and change name, in which case the `download` attribute doesn't work – blisher Jan 25 '16 at 14:17
  • @blisher What do you mean for local files? It does download the remote file and change the name according to `download` attribute value. – Dmitri Pavlutin Jan 25 '16 at 14:19
  • If you change `href` attribute to `http://lorempixel.com/g/400/200/` in the demo it doesn't work anymore – blisher Jan 25 '16 at 14:32