从Node.js中的Buffer解析multipart / form-data [英] Parse multipart/form-data from Buffer in Node.js

查看:830
本文介绍了从Node.js中的Buffer解析multipart / form-data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Buffer,我知道这是一个 multipart / form-data 有效负载,我也知道HTTP Content-Type 提前标题,包括边界。

I have a Buffer which I know is a multipart/form-data payload, and I also know the HTTP Content-Type header in advance, which includes the boundary.

有一些模块,例如node-formidable只能在http请求流上运行,所以我感兴趣的是in是如何同步解析多部分有效负载?

There are modules such as node-formidable which work only on http request streams, so what I'm interested in is how to parse a multipart payload synchronously?

推荐答案

查看源代码 form.parse() ,您应该能够模仿其内部所做的大部分工作。

Looking at the source for formidable's form.parse(), you should be able to do mimic most of what it's doing internally.

另一种解决方案可能会使用 busboy 之类的内容你要写一个普通的旧解析器流,所以你最终会得到类似的东西:

Another solution might be to use something like busboy which gives you a plain old parser stream to write to, so you might end up with something like:

var Busboy = require('busboy');
var bb = new Busboy({ headers: { 'content-type': '....' } });

bb.on('file', function(fieldname, file, filename, encoding, mimetype) {
  console.log('File [%s]: filename=%j; encoding=%j; mimetype=%j',
              fieldname, filename, encoding, mimetype);
  file.on('data', function(data) {
    console.log('File [%s] got %d bytes', fieldname, data.length);
  }).on('end', function() {
    console.log('File [%s] Finished', fieldname);
  });
}).on('field', function(fieldname, val) {
  console.log('Field [%s]: value: %j', fieldname, val);
}).on('finish', function() {
  console.log('Done parsing form!');
});

bb.end(someBuffer);

这篇关于从Node.js中的Buffer解析multipart / form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆