继我之前的问题 here ,
我有一个使用 Electron 平台和 Javascript 的桌面应用程序,我使用以下方法将 HTML5 Canvas 转换为 JPEG:
<a id="download" download="Path.jpg">Download JPG</a>
然后,
function download(){
var dt = canvas.toDataURL('image/jpeg');
this.href=dt;
}
document.getElementById('download').addEventListener('click', download, false);
这刷新了我的整个应用程序。如何更改此行为,以便在单击下载属性时页面不刷新?
最佳答案
我可以想到这两个片段,一个使用 blob,一个使用 download 元素。外部图书馆:FileSave.js
// this one use FileSaver.js library
canvas.toBlob(function(blob) {
saveAs(blob, "pretty image.png");
});
// or this way using download element.
// here you can encode your image-data and then send it.
var download = document.getElementById('download');
download.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(imageData));
download.setAttribute('download', 'file.jpg');
我刚才也找到了这个, Electron 特定的解决方案: Saving files locally with electron
关于javascript - 下载属性的 Electron 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47827835/