簡體   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