Koa: How to upload file in koa2?

Created on 28 Mar 2017  Â·  16Comments  Â·  Source: koajs/koa

And most important, with a callback when upload finish, like jquery-file-upload-middleware in express.
Getting params is also needed.

Most helpful comment

I summarize the best choise of koa2 middleware here.
koa-router --- for router and work with koa-multer
koa-multer --- for upload file
koa-bodyparser --- for parse json and form
koa-send --- for download file
koa-session --- for session
koa-sslify --- forced change to https
koa-favicon
koa-logger
koa-static

All 16 comments

@carlisliu Thank you very much. But there should have a plugin to do that.

@SteveCruise you mean like https://github.com/koa-modules/multer?

@fl0w koa-better-body is based on Formidable to deal with file upload. But Formidable is hard to use. jquery-file-upload-middleware is based on Formidable and makes it easy but it's only for express. Multer is not the right one.

Formidable is hard to deal with multiple upload.

here is another promise based file uploader I'm using that in my project
https://github.com/m4nuC/async-busboy

I use koa multer and works pretty well.

@BruceHem Yes, koa-multer is what I want, but it has little readme, I must read multer for express first.

I summarize the best choise of koa2 middleware here.
koa-router --- for router and work with koa-multer
koa-multer --- for upload file
koa-bodyparser --- for parse json and form
koa-send --- for download file
koa-session --- for session
koa-sslify --- forced change to https
koa-favicon
koa-logger
koa-static

@valVk Thank you very much.

I also had some struggle, thus wrote my own wrapper.

@valVk Thanks this one is really easy to use.

@crazyyi Hi, what the best of it for upload file? What should I use?

What can I use to upload files directly as binary stream? Like with AWS S3 (and minio)?

function store(ctx) {
    const form = new formidable.IncomingForm(...);
    form.parse(ctx.req, (err, fields, files) => {
        if (err) {
             ...
        }
        ...
    })
}

Exactly my question, @std4453. I was trying to scavenge around, searching for a solution - but failed to find a single mention to that use case. multipart/form-data encoding only makes sense in the context of HTTP forms.

The way I usually go about this is using raw-body myself in a custom middleware and messing with the request body as a Buffer.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ElegantScripting picture ElegantScripting  Â·  5Comments

dounine picture dounine  Â·  4Comments

tvq picture tvq  Â·  4Comments

TheRav3n picture TheRav3n  Â·  3Comments

edahlseng picture edahlseng  Â·  3Comments