cavas 图片保存到本地

直接上代码:
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);
};

你可能感兴趣的:(cavas 图片保存到本地)