Triggering the standard Save dialog

I have a file, a non-ruby executable or zip file, and I want to trigger the standard "Save file As" dialog.

What is the RoR way to do this?

- - - -

I have scoured the net and the only think I can come up with is some Javascript that gets triggered when the user clicks on an image.

function my_onclick() {     alert('This is an alert');     var url='/LaunchBrowser.zip';     // window.location = url     window.open(url,'Download'); };

The javascript runs but it opens a new browser tab/window with the following error:

  Routing Error

  No route matches "/LaunchBrowser.zip" with {:method=>:get}

- - - -

Surely there must be a simple way to enable the user to download a file that they want to get and that I want to give them.

What's wrong with putting it somewhere under /public and providing that URL?

Re[2]: [Rails] Triggering the standard Save dialog Hassan,

Friday, November 19, 2010, 10:37:36 AM, you wrote:

I don't understand. I apologize for being such a novice.

I am also running webrick for testing. Does that make a difference?

Surely there must be a simple way to enable the user to download a file that they want to get and that I want to give them.

> What's wrong with putting it somewhere under /public and providing > that URL?

I don't understand. I apologize for being such a novice.

Files under RAILS_ROOT/public are directly accessible -- like your app's javascript, css, or image files.

I am also running webrick for testing. Does that make a difference?

Nope.

Directly accessible how?

What do you recommend as the way to trigger the save dialog?

Directly accessible how?

Just provide a link with that URL. This is a basic web operation.

What do you recommend as the way to trigger the save dialog?

If the file has a .zip or .exe extension, it should be automatic.

Hassan Schroeder wrote in post #962641:

Yes. A standard web hyperlink.

Hassan Schroeder wrote in post #962645:

Just provide a link with that URL. This is a basic web operation.

What kind of link? <a ...> ... </a>

Yes. A standard web hyperlink.

In other words: there's nothing magical about HTML files or images. The browser is smart enough to do the right thing with any content you provide -- even if that "right thing" is saving for use with another application.

The reason that it's so hard to explicitly trigger a Save dialog in the browser is that there's never any need to do so!

-- Hassan Schroeder ------------------------ hassan.schroeder@gmail.com twitter: @hassan

Best,

I am so frustrated. My HAML code is

  %div{'id' => becca_chap_1_human_img_div}      %a{'id' => download_file_a, 'href' => '/public/LaunchBrowser.zip'}        = image_tag('PhotosOfBecca.png', :'id' => becca_chap_1_human_img)

The HAML parses correctly ... Yet when I click on the image I get a routing error   Routing Error

  No route matches "/public/LaunchBrowser.zip" with {:method=>:get}

What am I doing wrong?

%a\{&#39;id&#39; =&gt; download\_file\_a, &#39;href&#39; =&gt; &#39;/public/LaunchBrowser\.zip&#39;\}

You could look at your css/js/image references as examples, as I said previously...

No route matches "/public/LaunchBrowser.zip" with {:method=>:get}

.. which would show you that '/public' is *not* part of the URL.

I'm sorry, Hassan, I still don't get it.

When I make that link, i.e.   /public/LaunchBrowser.zip why is the link going through rails routing?

If I make the link   LaunchBrowser.zip then rails seems to want the link to be   http://localhost:3000/marketing/LaunchBrowser.zip which makes sense because the the file setting up the link to LaunchBrowser.zip is   F:\Ralph-Rails-Apps\UltraDedup\app\views\marketing\demodownload.haml

Again, sorry to be so dense.

Because you haven't made it "/LaunchBrowser.zip".

Have you *looked* at any of your standard css/js/image file URLs that currently live under RAILS_ROOT/public? Pick one. Look at the generated markup for that. If that doesn't answer your questions, ask again, pasting it in as an example.