cavas 图片保存到本地

2018-01-05  本文已影响16人  千罹

直接上代码:
base64参数来源:

canvas.toDataURL("image/png");
function downloadFile(fileName, base64) {
  const aLink = document.createElement('a');
  const event = document.createEvent("MouseEvents");
  event.initEvent('click', false, false); // initEvent 不加后两个参数在FF下会报错
  aLink.download = fileName;
  aLink.href = base64;
  aLink.dispatchEvent(event);
}

es6 export:

export default (fileName, base64) => {
  const aLink = document.createElement('a');
  const event = document.createEvent("MouseEvents");
  event.initEvent('click', false, false); // initEvent 不加后两个参数在FF下会报错
  aLink.download = fileName;
  aLink.href = base64;
  aLink.dispatchEvent(event);
};
上一篇 下一篇

猜你喜欢

热点阅读