Multer无法看到从客户端发送的文件

时间:2019-11-25 11:01:01

标签: node.js multer multer-s3

我正在尝试将一个.3gp文件从React-Native发送到Node后端。我可以上传图像和视频,但Multer似乎看不到任何音频文件。这是我所拥有的:

// building formdata

const createAudioFormData = (uri, size) => {
    const data = new FormData();

    data.append(
        "body",
        JSON.stringify({
          uri
          size
        })
    );

    data.append("audio", {
        name: "test-audio",
        type: "multipart/form-data",
        uri
    });

    return data;
};
// sending formdata

const sendAudioToServer = audioData => {
  try {
    console.log("audioData", audioData);
    const { data } = await axios.post(`${GATEWAY}/api/uploads/audio`);

    console.log("audio data!", data);
  } catch (err) {
    console.log(
      "err caught sending file to backend!",
      err.response?.data ?? err
    );
  }
};
// backend receiving formdata

const singleAudioUpload = multer({
  storage: multerS3({
    s3: s3,
    bucket: BUCKET,
    contentType: multerS3.AUTO_CONTENT_TYPE,
    acl: "public-read",
    key: function(req, file, cb) {
      file.newUUID = uuid.v4();
      cb(null, `recordings/${file.newUUID}.3gp`);
    }
  })
}).single("audio");

router.post("/audio", auth, async (req, res) => {
  console.log("entered to audio!");

  singleAudioUpload(req, res, async error => {
    if (error) {
      console.log("error saving", error);

      return res.status(500).json({ msg: "Error caught uploading!" });
    } else if (req.file === undefined) {
      return res.status(500).json({ msg: "No file found to upload!" });
    } else {
      console.log("file!", req.file);

      return res.json({ msg: "File uploaded!" })
    }
  });
});

它一直在打印No file found to upload! ...我在做什么错了?

0 个答案:

没有答案