fileinfo.controller.ts 1.2 KB
Newer Older
Lee Soobeom's avatar
Lee Soobeom committed
1
2
3
4
5
6
import formidable from "formidable";
import { asyncWrap } from "../helpers/asyncWrap";
import { TypedRequest } from "../types";

export const uploadFile = asyncWrap(async (reqExp, res, next) => {
  const req = reqExp as TypedRequest;
Kim, MinGyu's avatar
Kim, MinGyu committed
7
8
9
10
11
  const form = formidable({
    multiples: false,
    uploadDir: "uploads",
    keepExtensions: true,
  });
Lee Soobeom's avatar
Lee Soobeom committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

  await new Promise((resolve, reject) => {
    form.parse(req, (err, fields, files) => {
      if (err) {
        reject(err);
        return;
      }
      console.log("fields", fields);
      console.log("files", files);
      req.body = fields;
      req.files = files;
      resolve(files);
    });
  });
  next();
  return;
});

export const uploadFiles = asyncWrap(async (reqExp, res, next) => {
  const req = reqExp as TypedRequest;
Kim, MinGyu's avatar
Kim, MinGyu committed
32
33
34
35
36
  const form = formidable({
    multiples: true,
    uploadDir: "uploads",
    keepExtensions: true,
  });
Lee Soobeom's avatar
Lee Soobeom committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

  await new Promise((resolve, reject) => {
    form.parse(req, (err, fields, files) => {
      if (err) {
        reject(err);
        return;
      }
      console.log("fields", fields);
      console.log("files", files);
      req.body = fields;
      req.files = files;
      resolve(files);
    });
  });
  next();
  return;
});