cojs / busboy

Busboy multipart parser as a yieldable
154 stars 24 forks source link

co-busboy was unable to parse ctx using multipart/form-data in koa2 #33

Open xinshouke opened 8 years ago

xinshouke commented 8 years ago

I plan to test a file upload using multipart/form-data in koa2, the below was my code:

var parts = parse(ctx.request);
// or var parts = parse(ctx);
var part;
while (part = await parts) {
var stream = fs.createWriteStream(path.join(os.tmpdir(), Math.random().toString()));
console.log('part===>', part);
part.pipe(stream);
console.log('uploading %s -> %s', part.filename, stream.path);
}

the below was the dump of ctx in log:

ctx===> { request:
   { method: 'POST',
     url: '/upload',
     header:
      { host: '127.0.0.1:3000',
        connection: 'keep-alive',
        'content-length': '2316',
        'cache-control': 'max-age=0',
        accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        origin: 'http://127.0.0.1:3000',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.277.400 QQBrowser/9.4.7658.400',
        'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryJl7OKIVqHwYSA5fz',
        referer: 'http://127.0.0.1:3000/',
        'accept-encoding': 'gzip, deflate',
        'accept-language': 'zh-CN,zh;q=0.8' } },
  response: { status: 404, message: 'Not Found', header: {} }

but I get the below error as below,my question is :how to get the uploading file name and the content of uploading file ? [TypeError: part.pipe is not a function]

haoxins commented 8 years ago

https://github.com/cojs/busboy/issues/30

luckydrq commented 8 years ago

you need await a promise while busboy doesn't support yet

lucaszong commented 7 years ago

const convert = require('koa-convert'); app.use(convert(function (next) { // the body isn't multipart, so busboy can't parse it if (!this.request.is('multipart/')) return yield next;

var parts = parse(this,{autoFields:true});
var part;
var fields={};
while (part = yield parts) {
    if (part.length) {
        // arrays are busboy fields
        fields[part[0]]=part[1];
          console.log('key: ' + part[0])
        console.log('value: ' + part[1])
    } else {
        // otherwise, it's a stream

    }
}
console.log('and we are done parsing the form!')

}));