Multiple File Image upload not working
Hello, I am trying to upload multiple images to cloudinary. My code works for one string image but for some reason when i am trying to loop through and upload multiple images I keep getting a "no such file or directory" error. For reference I am using a base64 encoded data.
Here is my code:
router.post("/:id", async (req, res) => {
try {
const ownerId = req.params.id;
const {
location,
built,
squareFeet,
rent,
capacity,
parkingStalls,
pets,
utilities,
bed,
bath,
post,
description,
tenant,
amenities,
images
} = req.body;
// Check owner id is valid
const owner = await User.User.findById(ownerId);
const { userType } = owner;
if (userType === "Landlord") {
let result = [];
if (typeof images === "string") {
const data = await cloudinary.uploader.upload(images, { folder: "properties" }, (err, result) => {
console.log(err);
});
result.push({
public_id: data.public_id,
secure_url: data.secure_url
});
} else {
for (let i = 0; i < images.length; i++) {
const data = await cloudinary.uploader.upload(images[i], { folder: "properties" }, (err, result) => {
console.log(err);
});
result.push({
public_id: data.public_id,
secure_url: data.secure_url
});
}
}
} catch (e) {
console.log(e);
}
Here is the error:
[Error: ENOENT: no such file or directory, open 'C:\Github Repository\smart-rental\server\data:image\gif;base64,R0lGODlh4wDjAJEAAAAAAP\wAAACH5BAEAAAIALAAAAADjAOMAAAL\hI+py+0Po5y02ouz3rz7D4biSJbmiabqyrbuC8fyTNf2jef6zvf+DwwKh8Si8YhMKp
fMpvMJjUqn1Kr1is1qt9yu9wsOi8fksvmMTqvX7Lb7DY\L5\S6\Y7P6\f8vv8PGCg4SFhoeIiYqLjI2Oj4CBkpOUlZaXmJmam5ydnp+QkaKjpKWmp6ipqqusra6voKGys7S1tre4ubq7vL2+v7CxwsPExcbHyMnKy8zNzs\AwdLT1NXW19jZ2tvc3d7f0NHi4+Tl5ufo6err7O3u7+Dh8vP09fb3+Pn6+\z9\v\w
8woMCBBAsaPIgwocKFDBs6fAgxosSJFCtavIgxo8aNMRw7evwIMqTIkSRLmjyJMqXKlSxbunwJM6bMmTRr2ryJM6fOnTx7+vwJNKjQoURbFQAAOw==,data:image\gif;base64,R0lGODlh4wDjAJEAAAAAAP\wAAACH5BAEAAAIALAAAAADjAOMAAAL\hI+py+0Po5y02ouz3rz7D4biSJbmiabqyrbuC8fyT
Nf2jef6zvf+DwwKh8Si8YhMKpfMpvMJjUqn1Kr1is1qt9yu9wsOi8fksvmMTqvX7Lb7DY\L5\S6\Y7P6\f8vv8PGCg4SFhoeIiYqLjI2Oj4CBkpOUlZaXmJmam5ydnp+QkaKjpKWmp6ipqqusra6voKGys7S1tre4ubq7vL2+v7CxwsPExcbHyMnKy8zNzs\AwdLT1NXW19jZ2tvc3d7f0NHi4+Tl5ufo6err7O3
u7+Dh8vP09fb3+Pn6+\z9\v\w8woMCBBAsaPIgwocKFDBs6fAgxosSJFCtavIgxo8aNMRw7evwIMqTIkSRLmjyJMqXKlSxbunwJM6bMmTRr2ryJM6fOnTx7+vwJNKjQoURbFQAAOw=='] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Github Repository\\smart-rental\\server\\data:image\\gif;base64,R0lGODlh4wDjAJEAAAAAAP\\wAAACH5BAEAAAIALAAAAADjAOMAAAL\\hI+py+0Po5y02ouz3rz7D4biSJbmiabqyrbuC8fyTNf2jef6zvf+DwwKh8Si8YhMKpfMpvMJjUqn1Kr1is1qt9yu9wsOi8fksvm
MTqvX7Lb7DY\\L5\\S6\\Y7P6\\f8vv8PGCg4SFhoeIiYqLjI2Oj4CBkpOUlZaXmJmam5ydnp+QkaKjpKWmp6ipqqusra6voKGys7S1tre4ubq7vL2+v7CxwsPExcbHyMnKy8zNzs\\AwdLT1NXW19jZ2tvc3d7f0NHi4+Tl5ufo6err7O3u7+Dh8vP09fb3+Pn6+\\z9\\v\\w8woMCBBAsaPIgwocKFDBs6fAg
xosSJFCtavIgxo8aNMRw7evwIMqTIkSRLmjyJMqXKlSxbunwJM6bMmTRr2ryJM6fOnTx7+vwJNKjQoURbFQAAOw==,data:image\\gif;base64,R0lGODlh4wDjAJEAAAAAAP\\wAAACH5BAEAAAIALAAAAADjAOMAAAL\\hI+py+0Po5y02ouz3rz7D4biSJbmiabqyrbuC8fyTNf2jef6zvf+DwwKh8Si8Yh
MKpfMpvMJjUqn1Kr1is1qt9yu9wsOi8fksvmMTqvX7Lb7DY\\L5\\S6\\Y7P6\\f8vv8PGCg4SFhoeIiYqLjI2Oj4CBkpOUlZaXmJmam5ydnp+QkaKjpKWmp6ipqqusra6voKGys7S1tre4ubq7vL2+v7CxwsPExcbHyMnKy8zNzs\\AwdLT1NXW19jZ2tvc3d7f0NHi4+Tl5ufo6err7O3u7+Dh8vP09fb3+Pn6
+\\z9\\v\\w8woMCBBAsaPIgwocKFDBs6fAgxosSJFCtavIgxo8aNMRw7evwIMqTIkSRLmjyJMqXKlSxbunwJM6bMmTRr2ryJM6fOnTx7+vwJNKjQoURbFQAAOw=='
}
0
-
Hi Gavin,
When using our Upload API, it is currently only supporting a single file for each upload. Although, you can use multi-threads (e.g., iterate through files from the frontend and do multiple POST to your backend for each file) to upload many files at once as the Upload API does have a high concurrency rate.
Hope this helps. Please let me know if you have any further questions.
0
Post is closed for comments.
Comments
1 comment