
现在系统获取到了一段动态生成的 sg 代码,怎么根据这段 string 类型,实际上就是 xml 类型的代码,生成文件啊?
1 yuankui 2019-12-05 14:17:52 +08:00 File.write(filename, svg) ? |
2 SakuraKuma 2019-12-05 14:27:39 +08:00 blob + download? |
3 yvettemuki OP |
4 finalwave 2019-12-05 14:43:03 +08:00 浏览器上把 string 转成一个 blob,创建一个 a 标签,把 blob 用 URL.createObjectURL 转换一下挂在 a.href 上,文件名挂在 a.download 上,触发一下 a 标签 click 事件,完事 |
5 yvettemuki OP @SakuraKuma 试过了不可以, 转的方式:const blob = new Blob([svg], {type: 'image/svg+xml'}) |
6 yvettemuki OP @finalwave 对 就是这个,但是报错了 orz This page contains the following errors: error on line 1 at column 1: Document is empty Below is a rendering of the page up to the first error. |
7 finalwave 2019-12-5 14:59:44 +08:00 @yvettemuki #6 You shouldn't be writing/outputting any HTML to the page when said page is being cast as an XML Document (header ("Content-Type:text/xml");). 后端生成的 xml 有问题吧 |
8 yvettemuki OP @finalwave 这是 mxGraph 框架导出来的 xml,不是后端生成的,神奇的是,这段 svg 代码放到一个新的静态 html 页面打开是正常显示的 转换成 blob 之后也打印出来了 |
9 yvettemuki OP @finalwave 哈哈哈哈 我解决了 是 mxGraph 用得有问题,谢谢啦 |