繁体   English   中英

NodeJs如何将Multer Buffer保存到系统上的文件中

[英]NodeJs how to save a Multer Buffer into a file on system

我正在使用 Multer 解析来自 Formdata 的音频文件 Multer 部分正在工作,我可以访问以下内容:

audioUri: [
    {
      fieldname: 'audioUri',
      originalname: 'audio.m4a',
      encoding: '7bit',
      mimetype: 'audio/m4a',
      buffer: <Buffer 00 00 00 18 66 74 79 70 6d 70 34 32 00 00 00 00 69 73 6f 6d 6d 70 34 32 00 00 04 dc 6d 6f 6f 76 00 00 00 6c 6d 76 68 64 00 00 00 00 dc 4c 17 fc 
dc 4c ... 42739 more bytes>,
      size: 42789
    }

我正在尝试将此文件保存到系统文件中。 对于图像,我通常使用清晰的图像,在这里我尝试使用 fs,如下所示

const newpath = `assets/audio/orders/${fileName}`;
    const readFile = util.promisify(fs.readFile);
    const data = await readFile(req.files.audioUri[0].buffer);
    const chunk = new Uint8Array(data);
    await fs.writeFileSync(newpath, chunk);

我收到TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes

显然不需要从缓冲区读取文件,您可以直接保存如下:

 const newpath = `assets/audio/orders/${fileName}`;
 fs.writeFileSync(newpath, req.files.audioUri[0].buffer);

希望这可以帮助某人!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM