用合理的文件名保存SVG中包含的BMP

问题描述 投票:1回答:2

我有一个SVG文件,该文件在svg:image中包含一个BMP作为数据URI。问题在于某些浏览器不会接受download="download.bmp"链接的svg:a属性。

<svg xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg" 
    onload="dd();" baseProfile="full" version="1.1" height="409.6" width="1036.8">
  <script type="text/ecmascript">
function dd() {
fetch(document.getElementById('thebmp').getAttribute('xlink:href')).
  then(res =&gt; res.blob()).then(res =&gt; window.URL.createObjectURL(res)).
    then(function(x) 
    { document.getElementById('theanchor').setAttribute('href', x);});
}
  </script>
  <g transform="scale(0.2)">
    <a href="#" download="download.bmp" id="theanchor">
      <image id="thebmp" height="2048" width="2592" y="0" x="163" 
        xlink:href="data:image/x-ms-bmp;base64,...."/>
    </a>
    <g transform="translate(163,0)">
      <g id="P_1_1">
        <path d="M 130 102 l 2332 0 l 0 1844 l -2332 0 l 0 -1844" style="fill:none; 
         stroke:#66ff66; stroke-width:6px; stroke-opacity:0.75;"/>
        <text y="379" x="107" font-size="75px" fill="#66ff66" text-anchor="end">[0]</text>
      </g>
    </g>
    <g transform="translate(3110,122)">
      <g transform="scale(122)">
        <text fill="#66ff66" y="0" x="0" font-size="0.9px">blah</text>
      </g>
    </g>
  </g>
</svg>

您可以在testsvgf.svg中找到完整版本为https://www.magentacloud.de/share/6x.svy3pz9

我希望用户能够将BMP图像另存为BMP文件(svg:img xlink:href的原始内容,并且最好向用户建议一个合理的文件名。

如所写,该代码仅在使用默认名称download.bmp提示保存的firefox(经过60和68测试)上有效。

[其他浏览器的行为有所不同...

Chrome提议/保存some-cryptic-filename-without-extension(版本80)

Edge:单击时不执行任何操作。从上下文菜单中选择Save link as时,将从(1).txt开始增加下载文件名(我可以使用上下文菜单)

在所有三个浏览器中,保存文件的内容都是完全正确的。仅仅从Chrome和Edge中,您就无法识别保存的内容。

例如添加document.getElementById('theanchor').setAttribute('download', 'another.bmp*);之后的document.getElementById('theanchor').setAttribute('href', x);无效

我做错了什么? -预先感谢!

javascript svg cross-browser
2个回答
2
投票

您没有做错任何事!

download属性在Scalable Vector Graphics (SVG) 2 draft中得到正式支持。不幸的是,这是一个草稿。像许多其他网络技术一样,官方规范并不一定总是运行得很快(SVG 1.1于2011年发布),许多浏览器都渴望尽早采用规范。

这正是caniuse.com存在的原因。顺便说一句,它表明svg:a元素的download属性为currently only supported by Firefox

您可以向尚不支持该功能的浏览器提出功能请求,或者等待<年,直到所有地方都支持该功能。仅供参考,download属性为added in June 2016

感谢您展示一个可以使用SVG进行的奇怪示例!


0
投票
您可以使用foreignObject和HTML锚元素,它们在Chrome中确实支持download属性。
© www.soinside.com 2019 - 2024. All rights reserved.