我正试图用MERN Stack将图像上载到AWS-S3,但我甚至可以回头去工作。 下面是:tutorial ,但我正在获得一种类型Error: 不能读懂未经界定的特性(现成转移-编码)。
我按照一些网站的建议,在我的表格中添加了<条码>encType=多部分/格式数据,但并未奏效。
I also did the relevant logs with debug
here and formData.entries()
produced the names of my files accurately:
const onImageChange = (event) => {
const selectedFiles = event.target.files;
setFiles(selectedFiles);
log("event.target.files %o", event.target.files);
log("selectedFiles %o", selectedFiles);
};
const handleSubmit = async (event) => {
event.preventDefault();
if (files.length === 0) {
return alert("please select an image");
}
const formData = new FormData();
log("formData %o", formData);
for (const file of files) {
formData.append("s3Images", file);
}
log("formData after append %o", formData);
for (const entry of formData.entries()) {
const [key, value] = entry;
console.log(`${key}:`, value);
}
const result = await fetch(BASE_URL + "/upload", {
method: "POST",
body: formData,
});
log("result %o", result);
const data = await result.json();
log("data %o", data);
难道我的神职人员有错吗?
const uploadWithMulter = () =>
multer({
storage: multerS3({
s3: s3,
bucket: BUCKET_NAME,
metadata: function (req, file, cb) {
cb(null, { fieldname: file.fieldname });
},
key: function (req, file, cb) {
cb(null, file.originalname);
},
}),
}).array("s3Images", 2);
const uploadToAws = (req, res) => {
const upload = uploadWithMulter();
upload((req, res, error) => {
if (error) {
res.json({ error, msg: "Error occurred while uploading images." });
return;
}
res.json({ msg: "Files uploaded successfully.", files: req.files });
});
};
router.post("/upload", uploadToAws);