javascript实现点击按钮下载图片的方法

2023-08-27 22:51:46
474

示例代码:

javascriptfunction downloadImage(imageUrl, imageName) {
    var a = document.createElement('a');
    a.href = imageUrl;
    a.download = imageName;
    a.click();
}

// 使用
javascriptdownloadImage('http://example.com/example.jpg', 'example.jpg');

这种方法可能受到浏览器的安全限制,特别是在跨域请求的情况下。如果图片服务器不允许文件下载,那么这种方法可能会失败。