使用Html和Javascript在浏览器中导出图像

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

我想为用户提供导出(本地保存)网页中的图像(svg)的功能,但不确定如何完成。我已经看到你可以用画布而不是常规图像来做到这一点。

示例代码:

<html>
   <body>
    <img src="/path/to/image.svg">
    <button>Export and Save</button>
   </body>
</html>
javascript html image
2个回答
1
投票

您可以使用该文件的链接:

<a href="/path/to/image.svg" download>Export and save</a>

如果您想要下载另一个文件名,也可以设置download =“Filename.svg”。


0
投票

或者,如果您想使用除<a>元素之外的其他元素(即button),那么您可以使用:

  <img id="myImage" src="/path/to/image.svg">
  <button onClick="window.open(document.getElementById('myImage').src)">Export and Save</button>

在服务器端,您必须发送适当的标头以强制在客户端进行下载,例如:

Content-Type: application/octet-stream
Content-Disposition: attachment; filename="image.svg"
© www.soinside.com 2019 - 2024. All rights reserved.