繁体   English   中英

如何从svg链接下载svg作为文件?

[英]How to download svg as a file from a svg link?

我在服务器上托管了一个svg链接。 svg由pygal库(一个python svg绘图库)创建, pygal链接发送到在object标签中呈现的UI。 现在,我希望用户下载此svg 下面是我的HTML

<object id="mysvg" data="path/to/file.svg"></object>

现在,我可以通过执行以下操作来访问svg中的内容

svgEl = document.querySelector("#mysvg")
svgContent = svgEl.contentDocument

现在我使用来自stackoverflow的答案将我的svg下载为文件https://stackoverflow.com/a/53054948/5550284

所以这是我下面所做的

function downloadSVG(svgData, filename) {
  /// Create a fake <a> element
  let fakeLink = document.createElement("a");
  /// Add image data as href
  fakeLink.setAttribute('href', 'data:image/svg+xml;base64,' + window.btoa(svgData));
  /// Add download attribute
  fakeLink.setAttribute('download', filename);
  /// Simulate click
  fakeLink.click();

}

svgEl = document.querySelector("#mysvg")
svgContent = svgEl.contentDocument
downloadSVG(svgContent, "test.svg")

但是当我下载文件并打开它时,我看到了

error on line 1 at column 1: Document is empty

我究竟做错了什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM