koa-modules / multer

Middleware for handling `multipart/form-data` for koa, based on Express's multer.
MIT License
264 stars 26 forks source link

multer is writing to `ctx.req` instead of `ctx.request` #14

Closed FWeinb closed 5 years ago

FWeinb commented 7 years ago

I think multer should write to ctx.request because that is the koa request object currently it is writing to the ctx.req which is the original node request object.

In my opinion this change would align the behaviour of this module with the koa ecosystem.

danegigi commented 7 years ago

I agree 100%

What I did right now is use koa-compose to combine the multer middlware and a simple middleware that copies ctx.req.files, ctx.req.body, & ctx.req.file to ctx.request. I know this is not the best way to do it but I can live with it until this is fixed.

QianSu commented 7 years ago

Upload pictures are binary data, how can I convert the picture file?

dominhhai commented 7 years ago

@QianSu Did you try to change the file extension?

QianSu commented 7 years ago

@dominhhai Brother, thank you for your advice, I have solved this problem

raphaelsoul commented 7 years ago

on my side, those below are always undefined:

the configuration like this:

import multer from 'koa-multer'
import bodyparser from 'koa-bodyparser'
const upload = multer({ dest: '/path/to/tmp' })

//use it globally
app.use(multer.any())
app.use(bodyparser())

I tried postman and html form with enctype="multipart/form-data", both had saved file with hashed name but I can't access to file infomation via ctx

the version is koa-multer@1.0.1 and koa@2.2.0.

Weired

magicdawn commented 7 years ago

Since pass ctx.req, ctx.next to an express middleware is kind of Hack. Why not delegate ctx.req.body / ctx.req.file / ctx.req.files to ctx.request but not the reverse 😂

techanon commented 7 years ago

I have made a pull request that simply exposes the respective objects to Koa's Request wrapper. #15 Additionally updated the test requirements to node 6 or greater as Koa 2.x specifies as its requirement.

Side note: I have tried changing this middleware to pass the ctx.request object to multer, but that failed due to the fact that multer uses the streaming properties (eg: req.pipe(busboy)) of the native Request object, whereas the Koa wrapper doesn't seem to have that exposed.

i5ting commented 7 years ago

@fundon can be merge that pr?

niftylettuce commented 5 years ago

Please see my comment at https://github.com/koa-modules/multer/issues/30#issuecomment-506837638 and close this issue. The official Koa organization has forked this repository and released it (with PR's merged) under @koajs/multer. Thank you.