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

Using formidable with browserify to parse multipart/form-data in the front-end. #381

Closed lithiumlab closed 4 years ago

lithiumlab commented 8 years ago

Is it possible to achieve this? I have research a lot yet couldn't find anything that works. I'm not sure if http is getting the 'http-browserify' treatment. Docs says it is used automatically.

var formidable = require('formidable');
var http = require('http');

then using browserify and producing a bundle.js, on index.html i have:

http.get({
  method:'POST',
  path: '/multipart_data'
}, function(res){
  console.log(JSON.stringify(res.headers))
  var form = new formidable.IncomingForm();
  form.parse(res, function(err, fields, files) {
    console.log('should not get an error, but i do.')
  });
});

So, far, this looks good enough: Headers content:

{"strict-transport-security":"max-age=31536000","server":"Werkzeug/0.11.5 Python/2.7.11","date":"Thu, 12 May 2016 15:29:03 GMT","content-length":"240125","content-type":"multipart/form-data; boundary=97a1daeb-c2dc-4329-985e-4e42bd767eba;"}

Error immediately after:

bundle.js:45 Uncaught TypeError: WriteStream is not a constructorFile.open @ bundle.js:45IncomingForm.handlePart @ bundle.js:300IncomingForm.onPart @ bundle.js:264parser.onHeadersEnd @ bundle.js:498callback @ bundle.js:791MultipartParser.write @ bundle.js:904IncomingForm.write @ bundle.js:244(anonymous function) @ bundle.js:210EventEmitter.emit @ bundle.js:18814Readable.read @ bundle.js:20657flow @ bundle.js:21002resume_ @ bundle.js:20983afterTickTwo @ bundle.js:21998Item.run @ bundle.js:19423drainQueue @ bundle.js:19393
bundle.js:45 Uncaught (in promise) TypeError: WriteStream is not a constructor(…)

Any suggestions or hint on what is wrong?

egirshov commented 8 years ago

I suppose http vs http-browserify is not an issue here - formidable does not use http module). On the other hand it uses quite a few other modules which should be replaced with appropriate browser version - most importantly stream, events,util. The rest (fs, crypto, path, os) most likely could be ignored but maybe some configuration for browserify would be needed still.

tunnckoCore commented 7 years ago

I'm not sure, but I noticed that you passed res to the formidable (it needs req) and maybe that is the problem.

badeball commented 7 years ago

Seeing as Formidable utilizes the fs module, this task becomes non-trivial. I imagine that you in theory can compile it using fs-level as a provider for fs and memdb as your level store. It would be easier if you could provide us with a working example that easily illustrates the problem (meaning it's easy to run).

GrosSacASac commented 4 years ago

What is the use case on the front end ? Closing to keep only relevant open issues but feel free to continue discussion.