使用委托的单击按钮下载文件JQuery [英] Download file JQuery with delegated click button
问题描述
已解决:查看下面标记为已解决的答案.谢谢.
SOLVED: View the answer below marked SOLVED. Thanks.
我正在尝试从单击时由url指定的位置下载文件.我正在使用委托,但是单击按钮时,浏览器将导航到文件的位置并显示内容.我希望点击触发浏览器的下载对话框.
I am trying to download a file from location specified by url on click. I am using a delegate but when the button clicks the browser navigates to the location of the file and displays the contents. I want the click to trigger the browser's download dialog.
这是代码:
$('#left').delegate("a", "click", function(event){
//alert("CLICKED");
event.preventDefault();
window.location.href = url;
});
有关此问题的所有帖子都说要使用"event.preventDefault();"但这对我不起作用.有人有什么建议吗?
All posts regarding this question says to use the "event.preventDefault();" but that does not work for me. Does anyone have any suggestions?
推荐答案
您可能需要更改服务器端(如果可以).
You may need to change the server side (if you are able to).
http响应必须包含标头:
The http response must contain the header:
Content-disposition: attachment; filename=fname.ext
因此,浏览器知道必须打开下载对话框.否则,浏览器将尝试打开/预览它.
So the browser knows that it must open the download dialog. Otherwise the browser will try to open/preview it.
更多信息:
这篇关于使用委托的单击按钮下载文件JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!