使用Multer上传图像-在图像旁边创建了其他斑点

问题描述 投票:0回答:1
const upload = multer({ dest: `${__dirname}/uploads/images` });

app.post(
  "/api/users/:id/uploadProfilePic",
  upload.single("image"),
  updateProfilePic
);

const updateProfilePic = async (req, res) => {
  const userId = req.param("id");
  if (userId && isNumber(userId)) {
    // When using the "single"
    // data come in "req.file" regardless of the attribute "name". *
    const tmpPath = req.file.path;

    // The original name of the uploaded file
    // stored in the variable "originalname". *
    const targetPath = `uploads/images/${req.file.originalname}`;

    /** A better way to copy the uploaded file. **/
    const src = fs.createReadStream(tmpPath);
    const dest = fs.createWriteStream(targetPath);
    src.pipe(dest);
    src.on("end", () => {
      res.status(200).send("complete");
    });
    src.on("error", err => {
      res.status(500).send(err);
    });
  }
};

在我的快速应用中,我有以下代码用于上传图像-这似乎已成功上传了图像,但它也在我的上载文件夹中创建了此数据Blob-multipart

node.js rest express multipart multer
1个回答
0
投票

您可以执行类似操作

const storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, 'uploads/')
    },
    filename: function (req, file, cb) {
      cb(null, file.originalname)
    }
  })

  const upload = multer({storage: storage})
© www.soinside.com 2019 - 2024. All rights reserved.