Javascript下载在firefox中停止,但在Chrome中有效

Javascript download stop in firefox, but works in Chrome

本文关键字:但在 Chrome 有效 下载 firefox Javascript      更新时间:2023-09-26

我的HTML页面中有一个简单的按钮,用于下载SVG文件。这在Chrome中运行得很好,但在Firefox中,文件只有1行(第一行)。

$("button").click(function(){
    svgSource = phylocanvas.getSvgSource();
    console.log(svgSource);
    if(svgSource){
        var hiddenElement = document.createElement('a');
        document.body.appendChild(hiddenElement); // Add the element to the DOM
        hiddenElement.setAttribute("type", "hidden"); // make it hidden
        hiddenElement.href = 'data:attachment/text,' + encodeURI(svgSource);
        hiddenElement.target = '_blank';
        hiddenElement.download = '<TMPL_VAR NAME="TREENAME">.svg';
        console.log(hiddenElement.download);
        hiddenElement.click();
    }
});

知道如何在Firefox中获取整个文件吗?

您也可以尝试:

var pom = document.createElement('a');
    pom.setAttribute('href', 'data:text/xml;charset=utf-8,' + window.encodeURIComponent(textContent));
    pom.setAttribute('download', filename);
var event = document.createEvent('MouseEvents');
    event.initEvent('click', true, true);
pom.dispatchEvent(event);

替换textContent和filename。

http请求可以包含一个与下载属性等效的头变量ContentDisposition。据我所知,Firefox和Chrome之间唯一的区别是,在Firefox中,标头属性具有优先级,而在Chrome中,下载属性获胜。也许在Chrome中使用了下载属性,并按原样下载文件,而在Firefox中使用了头属性(如果提供),并有一个???值,该值只保存文件的xml头,同时将内容发送给svg呈现器。(只是猜测;-)

相关文章: