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
7.06k stars 682 forks source link

Parse multipart file using formidable #355

Closed krusk closed 4 years ago

krusk commented 9 years ago

Hi all,

I am receiving a multipart file via a REST service call to a vendors API. This file includes XML data and blob data for two images. I need to be able to split and store these as individual files using Node/Express. I have seen a lot posts/resources on multipart form data, but my needs are parsing this using JavaScript into individual files that can then be uploaded into Azure Blob Storage. I suspect Node/Express or a Node module such as request (https://github.com/request/request) would be the right way to go, but haven't found anything concrete. Here is an example of the multipart file. Note that there is not filename for the multipart file:

MIME-Version:1.0
Content-Type:multipart/mixed; 
boundary="----=_Part_4_153315749.1440434094461"

------=_Part_4_153315749.1440434094461
Content-Type: application/octet-stream; name=Texture_1.png
Content-ID: response-1
Content-Disposition: attachment; filename=Texture_1.png

‰PNG
"blob data here"

------=_Part_4_153315749.1440434094461
Content-Type: application/octet-stream; name=manifest.xml
Content-ID: response-2
Content-Disposition: attachment; filename=manifest.xml

<?xml version="1.0"?>
<dae_root>blank_3D.dae</dae_root>

------=_Part_4_153315749.1440434094461
Content-Type: application/octet-stream; name=texture_3D.dae
Content-ID: response-3
Content-Disposition: attachment; filename=texture_3D.dae

<xml data here... lots of xml data>

------=_Part_4_153315749.1440434094461
Content-Type: application/octet-stream; name=Texture_0.png
Content-ID: response-4
Content-Disposition: attachment; filename=Texture_0.png

‰PNG
"blob image data"

Is this something that Formidable can manage, and if so, how?

Thanks,

K.

lithiumlab commented 8 years ago

I'm behind exactly the same use case i still can't find the answer clearly. Have you made some progress on this?

I want to able to parse multipart/related response from xhr in the front-end to inmmediately play an audio file and show some metadata that comes in json from the API.

It looks like formidable can deal with it, but theres a lot of code for file handling, storing, and connecting / integrating the with node servers that generates errors when trying to use it clientside in a browser.

GrosSacASac commented 4 years ago

Check this example https://github.com/node-formidable/node-formidable/blob/master/example/multipartParser.js. it shows how to use the multipart parser as standalone

Prinzhorn commented 3 years ago

The example is now at https://github.com/node-formidable/formidable/blob/master/examples/multipart-parser.js

But how do I adapt the code to use it with formidable that I've installed?

Replacing the require with const { MultipartParser } = require('formidable/lib/multipart_parser.js'); results in

multipartParser.on('data', ({ name, buffer, start, end }) => {
                ^

TypeError: multipartParser.on is not a functio

And it looks like index.js only exports IncomingForm. So how do I use the parser standalone?

GrosSacASac commented 3 years ago

const { MultipartParser } = require('formidable');

GrosSacASac commented 3 years ago

or const MultipartParser = require('formidable/src/parsers/Multipart.js');

GrosSacASac commented 3 years ago

lib does not exist

Prinzhorn commented 3 years ago

lib does not exist

I assume it must be created during the publish process

docker run --rm -it node:14 /bin/bash
npm i formidable
cd node_modules/formidable/
ls
# LICENSE  README.md  benchmark-2020-01-29_xeon-x3440.png  lib  package.json
ls lib/
# file.js  incoming_form.js  index.js  json_parser.js  multipart_parser.js  octet_parser.js  querystring_parser.js
GrosSacASac commented 3 years ago

install from master branch npm i github:node-formidable/formidable

Prinzhorn commented 3 years ago

@GrosSacASac I understand that this is possible and I can also do npm install formidable@canary but I'm under the impression that "canary" means not production?

Edit: But I understand now, the example code is meant to be used with the latest release and I probably have to adjust it for the stable release. Thanks!