node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7k stars 680 forks source link

Buffer usage #962

Closed R0N1n-dev closed 5 months ago

R0N1n-dev commented 6 months ago

Support plan

Context

What are you trying to achieve or the steps to reproduce?

import cosmic from "~~/lib/cosmic.mjs";
import formidable from "formidable";

export default defineEventHandler(async (event) => {
  if ((event.node.req.method = "POST")) {
    const form = formidable({});
    await form.parse(event.node.req, (err, fields, files) => {
      if (err) {
        console.log(err);
        return;
      }
      console.table(files?.file[0]);
      const media = /*files?.file[0]*/ {
        originalname: files?.file[0].originalFilename,
        buffer: files?.file[0].buffer,
      };
      //console.log(media);
      return { media };
    });
    /*console.log(media);
    try {
      const data = await cosmic.media.insertOne({
        media: media,
        folder: "nuxt-test",
      });
      return { Success: true, data };
    } catch (error) {
      console.log(error.message);
    }*/
  }
});

What was the result you got?

None. Coz I cannot get a buffer

What result did you expect?

A buffer Ican use to upload the image coz the cosmic API need a buffer

GrosSacASac commented 5 months ago

Search for similar issues and examples

tunnckoCore commented 3 months ago

@R0N1n-dev

I think you can check this example https://github.com/node-formidable/formidable/blob/master/examples/forceBuffer.js

Or you can try formidable-mini which is trimmed down version of formidable - just the parser and exposes a FormData/File API, thus meaning the "files" there are regular Web API Files, so you can file.arrayBuffer() (arraybuffer/uint8array) to Cosmic.