Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
xinshouke opened this issue May 10, 2016 · 3 comments
Open

Comments

@xinshouke
Copy link

xinshouke commented May 10, 2016

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
Copy link
Member

haoxins commented May 10, 2016

#30

@luckydrq
Copy link

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

@lucaszong
Copy link

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!')

}));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants