尝试在某些浏览器中使用JavaScript下载文件,而其他浏览器则无法使用
我有一个无法解决的问题。以下功能输入文件路径和名称并下载文件。它适用于Chrome和Edge浏览器,但使用Vivaldi和Pale Moon浏览器,它将使用一次,但不行。您需要重新加载浏览器才能再次工作。我已经将问题隔离到了这个功能。该警报适用于所有浏览器(第一次和第二次),并显示适当的文件路径和名称,但在第一次成功尝试后没有下载。控制台没有错误。我认为也许浏览器不喜欢第二次的“ var a”的创建,因此包括'a.remove()',但没有区别。
function StartDownload(filePath, fileName) { // this is the function to download the book. Creates an html object 'a', adds the url to it, and then downloads it
//alert("got here: " + filePath + " fileName is: " + fileName);
var a = document.createElement("a");
a.href = fileUrl;
a.setAttribute("download", fileName);
a.click();
//a.remove();
}
谁能提出可能的原因,为什么此代码第一次之后不会在这些浏览器中下载?
I have a problem I can’t solve. The following function inputs a file path and name and downloads the file. It works for chrome and edge browsers but with Vivaldi and pale moon browsers it will work once but not a second time. You need to reload the browser for it to work again. I have isolated the problem to this function alone. The alert works for all browsers, (first and second times) and shows the appropriate file path and name but no download takes place after the first successful attempt. There is no error in console. I thought that perhaps the browser didn’t like the creation of ‘var a’ a second time so included the ‘a.remove()’ but it makes no difference.
function StartDownload(filePath, fileName) { // this is the function to download the book. Creates an html object 'a', adds the url to it, and then downloads it
//alert("got here: " + filePath + " fileName is: " + fileName);
var a = document.createElement("a");
a.href = fileUrl;
a.setAttribute("download", fileName);
a.click();
//a.remove();
}
Could anyone suggest possible reasons why this code won’t download in those browsers after the first time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论