jQuery 从按钮下载 zip 文件?

发布于 2024-09-02 01:11:26 字数 309 浏览 4 评论 0原文

相当尴尬的是,我花了多少时间试图从按钮下载 zip 文件......

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


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

我在这里需要一些防弹的东西,这就是我在这里问的原因,谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

や莫失莫忘 2024-09-09 01:11:27

即发送 AJAX 请求。

我还没有尝试过,但理论上应该可行。如果您尝试转到已下载文件的位置,它会提示您下载,而不是将您带到该页面。

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


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

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";
})
︶葆Ⅱㄣ 2024-09-09 01:11:27

参加聚会有点晚了,但我想我会像其他人一样分享我的解决方案。

您可以两全其美;一个简单的HTML链接和一个JS功能,不会失去非JS用户。

<a id="download" href="http://www.example.com/download.zip">Download</a>

$("#download").on("click", function(e) {

    // cancel the link's original functionality
    e.preventDefault();

    // do something before downloading
    alert("Thanks for downloading!");

    // download file
    location.href = "http://www.example.com/download.zip";

});

Bit late to the party but thought I'd share my solution as nobody else has.

You can have the best of both worlds; a simple HTML link and a JS function, without losing the non-JS users.

<a id="download" href="http://www.example.com/download.zip">Download</a>

$("#download").on("click", function(e) {

    // cancel the link's original functionality
    e.preventDefault();

    // do something before downloading
    alert("Thanks for downloading!");

    // download file
    location.href = "http://www.example.com/download.zip";

});
握住我的手 2024-09-09 01:11:26

使用简单的:

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

链接。然后,即使没有 JavaScript 可用,它也能正常工作(甚至“防弹”)。它还提供了用户可能期望从下载链接中获得的更多传统功能,例如右键单击另存为或拖放。

当然,您可以使用 CSS 使其看起来像按钮而不是链接。但它实际上是一个链接。所以这就是它应该被标记的方式。

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.

简单气质女生网名 2024-09-09 01:11:26

您应该设置 location.href 属性来进行导航:

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

您还可以有一个简单的 元素,其样式就像一个按钮一样,这样甚至禁用 JavaScript 的用户将能够下载该文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文