# Download image in rails with javascript/jquery

**URL:** <https://www.ruby-forum.com/t/download-image-in-rails-with-javascript-jquery/221692>\
**Category:** Rails\
**Created:** [July 26, 2012, 8:26am UTC](https://www.ruby-forum.com/t/download-image-in-rails-with-javascript-jquery/221692 "2012-07-26T08:26:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [July 26, 2012, 8:26am UTC](https://www.ruby-forum.com/t/download-image-in-rails-with-javascript-jquery/221692/1 "2012-07-26T08:26:16Z")

</div>

in my rails app, i have got one URL \*  
([http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=code&chld=H|0](http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=code&chld=H%7C0))\*in  
Javascript

_view.html.erb_  
\*  
\*  
\*

 \<%= link\_to "QR","#", :onclick =\> "showCode();"%\>
- 

I need to download this image via click this Save button, I have use  
this  
way, But it will prompt one window, not downloading  
Is there any way to do this?

Thank you  
vishnu

---

<div class="post-metadata">

**Author:** ![amruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/bbe5ce/32.png) [@amruby](https://www.ruby-forum.com/u/amruby)\
**Post date:** [July 26, 2012, 4:07pm UTC](https://www.ruby-forum.com/t/download-image-in-rails-with-javascript-jquery/221692/2 "2012-07-26T16:07:52Z")

</div>

On Jul 26, 2012, at 2:24 AM, amruby wrote:

> imageURL =  
> “[http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=code&chld=H|0](http://chart.apis.google.com/chart?cht=qr&chs=300x300&chl=code&chld=H%7C0)”;  
> $(“#qr”).html(“Save”);  
> }
> 
> I need to download this image via click this Save button, I have use this way,  
> But it will prompt one window, not downloading  
> Is there any way to do this?

If you want a link to download a file from JavaScript, handle the  
onclick event and return a full page redirect to the file. It won’t  
actually navigate the page to the new URL unless the URL is something  
that a browser can actually show (the file will just download). You  
mention an image – you will probably need to set Rails to send this  
file as an attachment, since the browser will believe that it can handle  
an image. Some browsers are unwilling to be led, though, and you will  
still show the image. In that case, I find that Zipping the file is a  
great way to force the issue.

Walter
