Javascript上的文件下载在Firefox上不起作用

File Download on Javascript does not work on firefox

本文关键字:Firefox 不起作用 文件下载 Javascript      更新时间:2023-09-26

写了这段代码,它在谷歌浏览器上运行良好,但在火狐上不起作用,你有线索吗?

预期的行为是,您将 xml 文本和名称作为参数传递,它会下载一个包含该 xml 文本和您发送的名称的 xml 文件,正如我所说,对于 chrome 是可以的,但对于 Firefox,它不会下载它。

/** * 从查询结果的选定行创建和下载文件 * @param XML文本 * @param文件名 */

函数创建和下载文件(xmltext,文件名){

var pom = document.createElement('a');
//creates a blob variable with the xml text
var bb = new Blob([xmltext], {type: 'text/xml'});
//sets the atribute href
pom.setAttribute('href', window.URL.createObjectURL(bb));
pom.setAttribute('download', filename);
//creates the download url
pom.dataset.downloadurl = ['text/xml', pom.download, pom.href].join(':');
pom.draggable = true; 
pom.classList.add('dragout');
//apply the click on to download the file
pom.click();

}

我有一个非常相似的问题,刚刚在stackoverflow上为我解答: 下载属性在火狐中不起作用

尝试在单击事件之前将元素添加到 DOM:

//apply the click on to download the file
document.body.appendChild(pom);
pom.click();
document.body.removeChild(pom);