views:

77

answers:

3

Quite embarrassing how much time I spend trying to get to download a zipfile from a button....

<button type='button' id='button-download'>download zipfile</button>


$("#button-download").live("click", function() {
    $.get("http://localhost/admin/zip/002140.zip"); // doesn't work?
})

I need something bullet proof here, that's why I ask here, thanks.

+3  A: 

You should set the location.href property to cause navigation:

$("#button-download").live("click", function() {
  location.href = "http://localhost/admin/zip/002140.zip";
});

You could also have a simple <a> element, styled as if it were a button, in that way even the users who have JavaScript disabled will be able to download the file.

CMS
Gee, location.href, it's time to go to bed!
FFish
Actually I have a problem, when using this or also window.location I am loosing my DOM created by jQuery. That's not an issue though what's worse the page goes -1, like hitting the back button? WTF??
FFish
+1  A: 

That is sending an AJAX request.

I haven't tried this, but it should work in theory. If you try to go to a location of a file that is downloaded, it prompts you to download rather than take you to that page.

<button type='button' id='button-download'>download zipfile</button>


$("#button-download").live("click", function() {
    window.location = "http://localhost/admin/zip/002140.zip";
})
Kerry
A: 

Use a plain:

<a href="http://localhost/admin/zip/002140.zip" id="button-download">download zipfile</a>

link. Then it'll work fine (“bullet proof” even) without JavaScript available. It also offers more of the traditional affordances users might expect from a download link, such as right-click-save-as, or drag-and-drop.

You can of course use CSS to make it look like a button instead of a link. But what it actually is, is a link. So that's how it should be marked up.

bobince
Thanks I am using this as the backup under "problems downloading?" It's in JavaScript but it's an admin panel and my users need JavaScript enabled anywhere.
FFish