JavaScript Canvas 下载

标签 javascript html file canvas

我从 Canvas 下载绘图时遇到问题。这是我的代码:

    function downloadCanvas(link, canvasId, filename) {
    link.href = document.getElementById(canvasId).toDataURL();
    const le = link.href;
    console.log(le);
    le.download = filename;
};
download.addEventListener('mousedown', function() {
    downloadCanvas(this, 'draw', 'Drawing.jpeg');
}, false);

控制台显示le的值,这是下载链接,但文件没有下载...这里出了什么问题?我是初学者。

最佳答案

您遇到的问题是您正在尝试下载链接位置,而您只需要下载链接文件。

要解决此问题,您应该将 const le = link.href; 更改为 const le = link;。但是,您也可以删除 const le 并将 le.download 更改为 link.download

希望有帮助!

编辑:

这是代码的清理版本:

function downloadCanvas(link, canvasId, filename) {
    link.href = document.getElementById(canvasId).toDataURL();
    link.download = filename;
};
download.addEventListener('mousedown', function() {
    downloadCanvas(this, 'draw', 'Drawing.jpeg');
}, false);

关于JavaScript Canvas 下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43380232/

相关文章:

javascript - 线程模块给出 channel 关闭错误

javascript - 使用 Google App Engine 使用精简的 JS 构建网站以进行调试和生产

c - C 基础题 : Reading a Subfile in C

html - 悬停时淡化 DIV - 即使悬停在前面的 DIV 上

使用 fscanf 计算文本文件中有多少个整数

bash - perl 脚本结束时会发生什么?

javascript - 检查并仅用 javascript 替换 div 字符串

javascript - 如果一个键相同,则创建新的对象数组,然后添加这些对象的其他键的值

html - Flexbox 和 CSS 问题 : not displaying as I intend

javascript - href ="javascript:"在新选项卡中打开