jQuery 从按钮下载 zip 文件?
相当尴尬的是,我花了多少时间试图从按钮下载 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
即发送 AJAX 请求。
我还没有尝试过,但理论上应该可行。如果您尝试转到已下载文件的位置,它会提示您下载,而不是将您带到该页面。
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.
参加聚会有点晚了,但我想我会像其他人一样分享我的解决方案。
您可以两全其美;一个简单的HTML链接和一个JS功能,不会失去非JS用户。
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.
使用简单的:
链接。然后,即使没有 JavaScript 可用,它也能正常工作(甚至“防弹”)。它还提供了用户可能期望从下载链接中获得的更多传统功能,例如右键单击另存为或拖放。
当然,您可以使用 CSS 使其看起来像按钮而不是链接。但它实际上是一个链接。所以这就是它应该被标记的方式。
Use a plain:
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.
您应该设置
location.href
属性来进行导航:您还可以有一个简单的
元素,其样式就像一个按钮一样,这样甚至禁用 JavaScript 的用户将能够下载该文件。
You should set the
location.href
property to cause navigation: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.