electron 文件另存为

// 保存文件
export const saveFilesByPath = (path) => {
  let fileName = getFileName(path.params);
  fs.readFile(path.params,(err,data)=>{
    if(err){
      console.log(err);
      return false;
    }
    dialog.showSaveDialog({
      title: "保存文件",
      defaultPath: fileName,
      properties: ['saveFile'],
      filters: [
        { name: 'All Files', extensions: ['*'] }
      ]
    }).then(async (result) => {
      let  fd = fs.openSync(result.filePath, 'w');
      fs.writeFileSync(fd,data);
      fs.closeSync(fd);
    }).catch(err => {
      console.log(err);
    });
  });
};
// 获取文件名字
export const getFileName = (fileUrl=''):string => {
  let u:any = '';
  if(fileUrl){
    if(url.parse(fileUrl)){
      u = url.parse(fileUrl);
      if(u.pathname){
        u = u.pathname.split('/').pop();
        return u;
      }
    }
 

你可能感兴趣的:(js,electron,javascript,前端)