简体   繁体   中英

NodeJs how to save a Multer Buffer into a file on system

I am using Multer to parse and Audio file coming from Formdata The Multer part is working and i have access to the following:

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
    }

I am trying to save this file into a system file. With images i usually use sharp and here i am trying to use fs as follows

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);

I am getting a TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes

apparently there was no need to read the file from a buffer and you can directly save as follows:

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

Hope this can help somebody!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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