我想为用户提供导出(本地保存)网页中的图像(svg)的功能,但不确定如何完成。我已经看到你可以用画布而不是常规图像来做到这一点。
示例代码:
<html>
<body>
<img src="/path/to/image.svg">
<button>Export and Save</button>
</body>
</html>
您可以使用该文件的链接:
<a href="/path/to/image.svg" download>Export and save</a>
如果您想要下载另一个文件名,也可以设置download =“Filename.svg”。
或者,如果您想使用除<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"