How can I download my upload PDF file?
I am using cloudinary by heroku & rails5.
My question is how can I download pdf files from my rails app.
<%= link_to section.pdf_file_url, download: "#{section.webtest.name}_#{section.name}" do %>
<i class="fa fa-file-pdf-o" aria-hidden="true"></i>
<%= section.name %>
<% end %>
this is my code and generate url as below.
<a download="section name" href="http://res.cloudinary.com/haltec5um/image/upload/v154700000/something_cloudinary_generate_code.pdf">
<i class="fa fa-file-pdf-o" aria-hidden="true"></i>
section_name
</a>
when you click this link , it will show pdf file on my browser . But not download.
so, how can I down load the file?
-
Hi Kojiro,
If you want to create a Cloudinary URL which will request that the specified file is handled by the web browser as a download, and should not be opened directly, we have a parameter you can set which will request this.
If you're using our SDK to create the URL, please add the 'flags' parameter and add 'attachment' as a value.
If you're creating your URLs manually, you can specify this flag in the URL with fl_attachment.For example, this link should open the sample image in our demo account: image
And this link should request that the browser treats the file as a download: imageYou can read more about this flag and the options (which include specifying the filename to use when saving the file) here: https://cloudinary.com/documentation/image_transformation_reference#flags_parameter
Please let me know if that helps, and if you have additional questions
2 -
HI Stephen,
I am using the following code to get a download of a multi-page PDF file, and am trying to use the PDF file within my Rails code. However, I get an error that says "undefined method ... for String." It seems Cloudinary is returning a string and not a file. How do I get that into a file?
pdf = Cloudinary::Downloader.download("file_name", :flags => :attachment)
I tried decoding the returned string using Base64, but I get a "dynamic assignment error."
0 -
Hi,
When adding the flag attachment to the file it will download it when accessing it. For example:
https://res.cloudinary.com/demo/image/upload/fl_attachment/multi_page_pdf.pdf
You can create an image tag:
cl_image_tag("multi_page_pdf.pdf", :flag=>"attachment")
Let me know if that can works,0 -
Thanks for the response Shirly. I got it to work with the following code:
download = Cloudinary::Downloader.download("public_id", :flags => :attachment)
# Create new File instance for writing in binary
pdf = File.new("path to file", "wb")
pdf.write(download)
pdf.closeIndeed, the flags => attachment works.
1 -
Great! Thank you for the update and sharing your code!
0 -
hello. can any one help me with understanding how we can attach this flag in react.
cause in react SDK of cloudinary, we have to use
<Image>
<Transform>
but as i want to download a pdf file, i can't set it like this. having trouble with this0 -
Hi Amit,
You can create an image component:
<Image publicId="sample.jpg" >
<Transformation flags="attachment:pretty_flower" fetchFormat="auto" />
</Image>It will create the URL: https://res.cloudinary.com/demo/image/upload/f_auto,fl_attachment:pretty_flower/sample.jpgHowever, inside an image, it will just show the image.0 -
hey @Shiley, thanks for your response
we get publicid, secure_url as response after uploading the data in cloudinary. but the public id you used here (sample.jpg) in the Image tag doesn't look like the one i got in my response (5464241126). so is that response public id is same as the Image public id you used here ( sample.jpg) or this public id is just a name?
and i got the same question for the attachment:pretty_flower also. is that pretty flower is just name or it has to be something which comes form the response after uploading?
as you can see in above image, i want to make the image as clickable link so that on click i can download it. but i am not getting anywhere.0 -
Hi Amit.
In order to achieve this please consider this code:
```<div><CloudinaryContext cloudName="demo"><a href="https://res.cloudinary.com/demo/image/upload/fl_attachment:myPdf/multi_page_pdf.pdf" download><Image publicId="multi_page_pdf.pdf"><Transformation fetchFormat="auto" /></Image></a></CloudinaryContext></div>```The ״multi_page_pdf״ is the public_id of the image you would like to display and download."myPdf" is the name you set for the attachment file's name (this is optional if it is not set then the original filename is used as the attachment file name).Let us know if this works for you and if you need any further assistance.1 -
thank you for your answer michal. as you can see in the picture below, this is my backend code where i am trying to upload and this is the response i am getting. so here in secure_url the flags are not mentioned that's why your above answer can't be implemented to my saved data in database.
so can you tell me if there is any way we can set flags of attachment in the sedcure_url while uploading itself. so that it gets saved like that in database and later while fetching that url, i can get an attachment.0 -
Hi Amit,
You can create a transformed image upon upload by using eager transformations and then receive the transformed secure URL in the response under the eager value.
You can read more about eager transformations here:
https://cloudinary.com/documentation/transformations_on_upload#eager_transformations
For example:
```
Map result = cloudinary.uploader().upload("samplePdf.pdf", ObjectUtils.asMap(
"eager", Arrays.asList(
new Transformation().flags("attachment"))));```
And the response will include:
```
eager=[{...secure_url=https://res.cloudinary.com/demo/image/upload/fl_attachment/v1601895305/samplePdf.pdf,
```
Let us know if this solves your issue
0 -
Thank you so much Michal, it worked for me. here's the actual code. if anyone needs this in nodejs
0 -
Great! Thanks for the update.
0 -
Amit can you please share the code that you used in frontend to display the pdf file and then download it
0
Post is closed for comments.
Comments
14 comments