[英]How to resolve the “Failed - Forbidden” error while creating and downloading a file in D3JS
I have created a genealogy application based on Mike Bostock's collapsible tree https://observablehq.com/@d3/collapsible-tree .我创建了一个基于 Mike Bostock 的可折叠树https://observablehq.com/@d3/collapsible-tree的家谱应用程序。
I am trying to download the tree as an SVG document using the example http://bl.ocks.org/curran/7cf9967028259ea032e8 my code works if I use the static data in dataURL as is in the example我正在尝试使用示例http://bl.ocks.org/curran/7cf9967028259ea032e8将树下载为 SVG 文档,如果我像示例中那样使用 dataURL 中的静态数据,我的代码就可以工作
$('#download-SVG').click(function() {
var dl = document.createElement("a");
document.body.appendChild(dl); // This line makes it work in Firefox.
dl.setAttribute("href", dataURL);
dl.setAttribute("download", "test.svg");
dl.click();
});
, however, if I use the modified code to use the SVG, I get a "Failed - Forbidden" error. ,但是,如果我使用修改后的代码来使用 SVG,则会收到“失败 - 禁止”错误。
$('#download-SVG').click(function() {
var dl = document.createElement("a");
document.body.appendChild(dl); // This line makes it work in Firefox.
dl.setAttribute("href", function (svg) {
var svgAsXML = (new XMLSerializer).serializeToString(svg);
return "data:image/svg+xml," + encodeURIComponent(svgAsXML);
});
dl.setAttribute("download", "test.svg");
dl.click();
});
I would appreciate support in identifying the reason for this.我很感激在确定原因方面的支持。
I have managed to download the file by doing the following changes:通过进行以下更改,我设法下载了该文件:
$('#download-SVG').click(function() {
var dl = document.createElement("a");
document.body.appendChild(dl); // This line makes it work in Firefox.
dl.setAttribute("href", svgDataURL(svg.node())); // pass the node
dl.setAttribute("download", "test.svg");
dl.click();
});
function svgDataURL(svg) {
var svgAsXML = (new XMLSerializer).serializeToString(svg);
var dataURL = "data:image/svg+xml," + encodeURIComponent(svgAsXML);
return dataURL;
}
The file downloads, but I am not able to open the file, which I suspect is due to the svg data content, but that is a different issue altogether.文件下载,但我无法打开文件,我怀疑这是由于 svg 数据内容造成的,但这完全是一个不同的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.