file.controller.ts 638 Bytes
Newer Older
Yoon, Daeki's avatar
Yoon, Daeki committed
1
2
import formidable from "formidable";
import { asyncWrap } from "../helpers/asyncWrap";
3
import { TypedRequest } from "../types";
Yoon, Daeki's avatar
Yoon, Daeki committed
4

5
export const uploadFile = asyncWrap(async (reqExp, res, next) => {
6
  const req = reqExp as TypedRequest;
Jiwon Yoon's avatar
Jiwon Yoon committed
7
  const form = formidable({ multiples: true, uploadDir: "uploads" });
Yoon, Daeki's avatar
Yoon, Daeki committed
8
9
10
11
12
13
14

  await new Promise((resolve, reject) => {
    form.parse(req, (err, fields, files) => {
      if (err) {
        reject(err);
        return;
      }
15
16
      console.log("fields", fields);
      console.log("files", files);
17
18
      req.body = fields;
      req.files = files;
Yoon, Daeki's avatar
Yoon, Daeki committed
19
20
21
22
23
24
      resolve(files);
    });
  });
  next();
  return;
});