[koa__multer] Allows to access the properties file and files through the koa context. (#40034)

* [koa__multer] Allows to access the file nad files through the koa contet.

* Extends the koa context and request by the fields file, files.

* Removes unecessary import
This commit is contained in:
Yves Kaufmann
2019-11-05 13:21:16 -08:00
committed by Nathan Shively-Sanders
parent 1cb82bdd9f
commit 6d1bed7dac
2 changed files with 23 additions and 0 deletions
+17
View File
@@ -19,6 +19,11 @@
* async function uploadFile(ctx: Koa.Context){
* let multerReq = <multer.MulterIncomingMessage>ctx.req;
* let files = multerReq.files;
*
* // You can also directly access the files property from the context object or request object:
* files = ctx.files;
* files = ctx.request.files;
*
* let baseFilePath: string = ctx.params.path || '';
* //...
* }
@@ -32,6 +37,18 @@
import * as Koa from 'koa';
import { IncomingMessage } from 'http';
declare module 'koa' {
interface DefaultContext {
file: multer.File;
files: multer.File[];
}
interface Request {
file: multer.File;
files: multer.File[];
}
}
declare namespace multer {
interface File {
/** Field name specified in the form */
+6
View File
@@ -13,6 +13,12 @@ const app = new Koa();
app.use(upload.single('avatar'));
app.use(upload.array('photos', 12));
app.use(async (ctx, next) => {
for (const file of ctx.request.files) {
console.info(`Received file: ${file.filename}`);
}
await next();
});
const cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }]);
app.use(cpUpload);