Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

last filename is missing from req.files while use of 'fileds' middleware. #1261

Open
greyHairChooseLife opened this issue Jun 17, 2024 · 0 comments
Labels

Comments

@greyHairChooseLife
Copy link

I was trying to

response which files are uploaded right after client uploaded multiple files.

Codes

API route to load my lovely multer middleware,

app.post("/uploadFiles", uploads, (req, res) => {
  try {
    const uploadedQueryFiles = req.files?.queryFiles?.map(
      console.log(file);
      (file) => file.filename
    );
    const uploadedTargetFiles = req.files?.targetFiles?.map((file) => {
      console.log(file);
      return file.filename;
    });
    res.status(200).json({
      message: "result of upload:",
      uploadedQueryFiles,
      uploadedTargetFiles,
    });
  } catch (err) {
    console.log(err);
    res.status(500).json({ message: "server error while uploading." });
  }
});

This gives me logs(from console.log in code above) like below, but the last one doesn't have filename or anything but fieldname.

{
  fieldname: 'targetFiles',
  originalname: 'originam file name.docx',
  encoding: '7bit',
  mimetype: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  destination: 'INPUT/target',
  filename: 'new file name.docx',
  path: 'INPUT/target/new file name.docx',
  size: 15882
}
{ fieldname: 'targetFiles' }
how I configured middleware
// QUERY 저장소
const queryStorage = multer.diskStorage({
  destination: (_req, _file, cb) => {
    // req.params등을 통해서 동적으로 폴더, 파일명을 생성할 수도 있다.
    // https://stackoverflow.com/questions/59646445/multer-create-folder-if-not-exist
    const path = "INPUT/query";
    fs.mkdirSync(path, { recursive: true });
    return cb(null, path);
  },
  filename: (_req, file, cb) => {
    cb(null, Buffer.from(file.originalname, "latin1").toString("utf8"));
  },
});

// TARGET(DATA) 저장소
const targetStorage = multer.diskStorage({
  destination: (_req, _file, cb) => {
    const path = "INPUT/target";
    fs.mkdirSync(path, { recursive: true });
    return cb(null, path);
  },
  filename: (_req, file, cb) => {
    cb(null, Buffer.from(file.originalname, "latin1").toString("utf8"));
  },
});

const queryDest = multer({
  storage: queryStorage,
  fileFilter: (_req, file, cb) =>
    file.fieldname === "queryFiles" ? cb(null, true) : cb(null, false),
});
const targetDest = multer({
  storage: targetStorage,
  fileFilter: (_req, file, cb) =>
    file.fieldname === "targetFiles" ? cb(null, true) : cb(null, false),
});

const uploads = (req, res, next) => {
  queryDest.fields([{ name: "queryFiles" }, { name: "targetFiles" }])(
    req,
    res,
    next
  );
  targetDest.fields([{ name: "queryFiles" }, { name: "targetFiles" }])(
    req,
    res,
    next
  );
};

There is a man facing same one

in stackoverflow

Unfortunately, he got no proper answer yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant