electron另存为图片

  1. 取dom元素的img路径

vue中使用ref获取

    // 保存到本地
    saveQrCode(item) {
      const currentSrc = this.$refs[item.id][0].children[0].currentSrc;
      savePictureAs(currentSrc, item.ename);
    },
  1. 设置方法
/**
 * @description: 图片另存为
 * @param {*currentSrc}  二维码图片路径 -img-src
 * @param { defaultPath} 文件默认保存名
 * @return { status} 是否保存成功
 */
const savePictureAs = (currentSrc, defaultPath = "") => {
  const { dialog } = require("electron").remote;
  const fs = require("fs");
  let base64 = currentSrc.replace(/^data:image\/\w+;base64,/, "");
  let dataBuffer = new Buffer(base64, "base64");
  let dia = dialog.showSaveDialog({
    title: "保存文件",
    defaultPath,
    filters: [{ name: "Custom File Type", extensions: ["png", "jpg"] }],
  });
  //点击取消时
  if (!dia) return;
  fs.writeFile(dia, dataBuffer, function(err) {
    if (err) {
      console.log(err);
    } else {
    }
  });
};

export { savePictureAs };

你可能感兴趣的:(electron)