How to save ArrayBuffer?

后端 未结 3 1883
北恋
北恋 2020-12-22 07:56

How to save an ArrayBuffer in json file? I use electron-config for this, but in config.json I found \"{}\". I try convert (code) ArrayBuffer to string, but then I can\'t con

3条回答
  •  情歌与酒
    2020-12-22 08:28

    Working Snippet Node 14.xx+

    Create a output directory

      let rootDir = process.cwd()
      console.log("Current Directory"+ rootDir)
    
      let outDir = './out/';
      console.log("Out Directory"+ outDir)
    
      if (!fs.existsSync(outDir)){
          fs.mkdirSync(outDir);
      }else{
          console.log("Directory already exist");
      }
       
      // Save the raw file for each asset to the current working directory
      saveArrayAsFile(arrayBuffer,  outDir+ "fileName"+ new Date().getTime()+".png")
    

    Save file function

    const saveArrayAsFile =  (arrayBuffer, filePath)=> {
          fs.writeFile(filePath, Buffer.from(arrayBuffer), 'binary',  (err)=> {
              if (err) {
                  console.log("There was an error writing the image")
              }
              else {
                  console.log("Written File :" + filePath)
              }
          });
    };
    

提交回复
热议问题