简体   繁体   English

如何从 Formidable 解析的 object 中提取 Blob 和 ArrayBuffer / TypedArray? Node.js

[英]How do I extract the Blob and the ArrayBuffer / TypedArray from the Formidable parsed object? In Node.js

Long story short, I used a DataForm object that contains a blob of an image as one of the parts and sent it to my node.js server into the formidable parser:长话短说,我使用了一个 DataForm object,其中包含一个图像块作为部分之一,并将其发送到我的 node.js 服务器,进入强大的解析器:

    //blob enters the files variable
    formParser.parse(req, (err, fields, files)=>{
        if(err)throw err; 


        const fileInfo = JSON.parse(fields.JSON);

        //figure out how to read the blob, how to get it's array buffer, then write to C disk
        console.log(files);

        //fs.writeFile() here




    })

How do I extract the Blobs ArrayBuffer from this persistent file in formidable that looks like this:我如何从这个看起来像这样的持久文件中提取 Blobs ArrayBuffer:

{
  Binary: PersistentFile {
    _events: [Object: null prototype] { error: [Function (anonymous)] },
    _eventsCount: 1,
    _maxListeners: undefined,
    lastModifiedDate: 2022-06-15T17:44:50.214Z,
    filepath: 'C:\\Users\\rnata\\AppData\\Local\\Temp\\f962bfe8ab505addfba1e4600',
    newFilename: 'f962bfe8ab505addfba1e4600',
    originalFilename: 'blob',
    mimetype: 'image/png, image/jpg, image/jpeg',
    hashAlgorithm: false,
    size: 373021,
    _writeStream: WriteStream {
      _writableState: [WritableState],
      _events: [Object: null prototype],
      _eventsCount: 1,
      _maxListeners: undefined,
      path: 'C:\\Users\\rnata\\AppData\\Local\\Temp\\f962bfe8ab505addfba1e4600',
      fd: 4,
      flags: 'w',
      mode: 438,
      start: undefined,
      autoClose: true,
      pos: undefined,
      bytesWritten: 373021,
      closed: false,
      [Symbol(kFs)]: [Object],
      [Symbol(kCapture)]: false,
      [Symbol(kIsPerformingIO)]: false
    },
    hash: null,
    [Symbol(kCapture)]: false
  }
}

What classes do I need, or what methods do I have to use, so that I can extract an arraybuffer and use it in fs.writeFile().我需要什么类,或者我必须使用什么方法,以便我可以提取一个数组缓冲区并在 fs.writeFile() 中使用它。

Thank you!谢谢!

I was having the same issue.我有同样的问题。 In my case I was be able to fix it in that way:就我而言,我能够以这种方式修复它:

  1. Get filepath in that PersistentFile object.获取该 PersistentFile object中的文件路径。
  2. import * as fs from "fs" ; import * as fs from "fs"
  3. const srcToFile = (src: string) => fs.readFileSync(src);

Send that filepath to srcToFile function. It is going to return to you a buffer/Object.将该文件路径发送到 srcToFile function。它将返回给您一个缓冲区/对象。

I was having hard time to upload my formdata's file to AWS S3.我很难将我的表单数据文件上传到 AWS S3。 After parsing the multiform data and converting the files to Buffer, I got be able to upload it.解析多格式数据并将文件转换为 Buffer 后,我就可以上传了。 I hope it works for you too.我希望它也对你有用。

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

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