[英]I want to upload image on server with uuid dynamically
hi I want to upload image on server dynamically with uuid every thing is ready I already upload Image on server and save it in folder but I want specific id of every image by using uuid here is index page:嗨,我想使用 uuid 在服务器上动态上传图像,一切都准备好了
const data = await new Promise((resolve, reject) => {
const form = new IncomingForm()
form.parse(req, (err, fields, files) => {
if (err) return reject(err)
console.log(fields, files)
console.log(files.file.filepath)
var oldPath = files.file.filepath;
!fs1.existsSync("./public/images/uploads/2021/") &&
fs1.mkdirSync("./public/images/uploads/2021/");
var newPath =
`./public/images/uploads/2021/${files.file.originalFilename}`;
mv(oldPath, newPath, function(err) {
});
res.status(200).json({ fields, files })
})
})
And this is my upload to server code:这是我上传到服务器的代码:
const [image, setImage] = useState(null);
const [createObjectURL, setCreateObjectURL] = useState(null);
const uploadToClient = (event) => {
if (event.target.files && event.target.files[0]) {
const i = event.target.files[0];
setImage(i);
setCreateObjectURL(URL.createObjectURL(i));
console.log("setting image")
console.log(image);
}
};
const uploadToServer = async (event) => {
const body = new FormData();
console.log("file", image)
body.append("file", image);
const response = await fetch("/api/upload", {
method: "POST",
body,
});
};
I use uuid to give id to each image but don't how to do it I hope someone make example code for me I import this:我使用 uuid 为每个图像提供 id 但不知道如何做我希望有人为我制作示例代码我导入这个:
import { v4 as uuid } from 'uuid';
const unique_id = uuid();
but now I cant understand where to use it.但现在我不明白在哪里使用它。
can you send unique_id to the server (attached with the body) and then give this property as _id: unique_id while uploading to the database.您可以将 unique_id 发送到服务器(附在正文中),然后在上传到数据库时将此属性作为 _id: unique_id 提供。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.