Javascript 如何使用Firefox浏览器在客户端下载文件

Javascript 如何使用Firefox浏览器在客户端下载文件,javascript,jquery,firefox,Javascript,Jquery,Firefox,我有以下代码,它允许我在客户端下载json文件,在Safari和Chrome中完美工作: function downloadObjectAsJson(exportObj, exportName){ var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj)); var downloadAnchorNode = document.createElement('

我有以下代码,它允许我在客户端下载json文件,在Safari和Chrome中完美工作:

function downloadObjectAsJson(exportObj, exportName){
  var dataStr = "data:text/json;charset=utf-8," + 
  encodeURIComponent(JSON.stringify(exportObj));
  var downloadAnchorNode = document.createElement('a');
  downloadAnchorNode.setAttribute("href", dataStr);
  downloadAnchorNode.setAttribute("download", exportName + ".json");
  downloadAnchorNode.click();
  downloadAnchorNode.remove();
}

但是在Firefox中什么都没有发生。没有错误或警告。没什么。上面的代码有什么Firefox无法使用的地方吗?

几个月前,但仍然有一个对我有效的解决方案:

    const fileUrl = window.URL.createObjectURL(file); 
    const link = document.createElement('a'); 
    link.setAttribute('href', fileUrl); 
    link.setAttribute('download', filename); 
    link.setAttribute('target', '_blank'); 
    document.body.appendChild(link); //Required for Firefox 
    link.click(); 
    link.remove(); 
    window.URL.revokeObjectURL(fileUrl);

缺少
document.body.appendChild(链接)//Firefox
部件需要。有了它,几个月后一切都会好起来的,但是,以下是对我有效的解决方案:

    const fileUrl = window.URL.createObjectURL(file); 
    const link = document.createElement('a'); 
    link.setAttribute('href', fileUrl); 
    link.setAttribute('download', filename); 
    link.setAttribute('target', '_blank'); 
    document.body.appendChild(link); //Required for Firefox 
    link.click(); 
    link.remove(); 
    window.URL.revokeObjectURL(fileUrl);

缺少
document.body.appendChild(链接)//Firefox
部件需要。有了它,一切都会好起来

您是否尝试过先将元素附加到DOM?是的,仍然没有。您是否尝试过先将元素附加到DOM?是的,仍然没有。