我正在尝试调用此新创建的 anchor 的点击:
$('.file-status').html("Finished downloading <a class='download' download href='#{fileEntry.toURL()}'>#{name}</a>")
$('.download').click()
但是点击事件没有被调用。不知道这是为什么?
我试图强制下载,而不是在浏览器中打开链接。就像 Dropbox 一样
编辑
这是代码,所以更清楚:
fileEntry.createWriter ((fileWriter) ->
fileWriter.onwriteend = (e) ->
$('.file-status').html("Finished downloading <a class='download' download href='#{fileEntry.toURL()}'>#{name}</a>")
$('.download').trigger('click');
fileWriter.onerror = (e) ->
console.log "Write failed: " + e.toString()
fileWriter.write blob
), errorHandler
更新:
因此,从下面的答案中了解到,这实际上是不可能的,除非服务器将带有标题 Content-disposition: Attachment 的数据发送给我。但在我看来,这对纯 HTML5 JS 应用程序来说是一个非常糟糕的解决方案,可能会离线。
所以我发现了这个,它处理这个问题 super 棒!效果很好。这是链接:
http://updates.html5rocks.com/2011/08/Saving-generated-files-on-the-client-side
希望这可以帮助那些想做同样事情的人,因为我相信有很多人!
最佳答案
尝试将点击事件绑定(bind)到 anchor 标记并调用点击事件。
$(".download").bind("click", function (e) {
var $a = $(this);
window.location = $a.attr("href");
});
$(".download").click();
关于click - 对新添加的 dom 元素调用单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14870650/