I need urgent help for uploading Videos
After I tried all to debug the "can't read req.file issue", I changed my strategy and try another code. Description : I am sending the data with FormData from my frontend via redux-toolkit to node. The whole way long I log my FormData and the data is everywhere correct.
I send as header :
const router = require("express").Router();
const Videos = require("../models/Videos");
const {verifyToken, verifyTokenAndAuthorization, verifyTokenAndAdmin} = require("../middleware/verifytoken");
///
const multer = require("multer");
const storage = multer.memoryStorage();
const upload = multer({storage:storage});
///
const cloudinary = require("../utils/cloudinary");
const path = require("path");
//create
const uploadVideo = upload.single('src');
router.post("/", uploadVideo, verifyTokenAndAuthorization, async (req,res)=>{
let buf = req.file.buffer.toString('base64');
try{
const result = await cloudinary.uploader.upload_large("%22data%3Avideo/mp4%3B%20base64%22" + buf, {
upload_preset: "Mern_redux-practice",
resource_type: "video",
}, function(error, result){
if(error){
console.log(error)
} else{
console.log(result);
}
}
);
const newVideos= new Videos({
cloudinary_id: result.public_id,
ressort: req.body.ressort,
theme: req.body.theme,
title:req.body.title,
src: result.secure_url,
})
const savedVideos= await newVideos.save();
res.status(200).json(savedVideos);
} catch(error){
res.status(403)
console.log(error)
throw new Error("Action failed");
}
});
-
Hi Roman,
When using Cloudinary SDK to perform the upload using Base64, you can use the actual string format, and there is no need to encode it to escape the especial characters. For example:
...
app.get('/api/uploadlargebase64', function(req, res) {
const base64Image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
const result = cloudinary.uploader.upload_large(base64Image, {
upload_preset: "your_upload_preset",
resource_type: "image"
}, function(error, result) {
if (error) {
console.log(error)
} else {
console.log(result);
}
}
);
res.send({
'uploadresponse': "Upload done!"
});
});
...The data scheme must follow this format:
data:[<mediatype>][;base64],<data>
Hope this helps.
-
Hi Eric, I didn't have access to the internet for two days, so I can only answer now. That doesn't work. What is this string behind base64? Does it have to be? I entered it, then my computer crashes. If I don't enter it, then I get endless base64 in my console and my computer also crashes so I can't see the error. Thanks for the help.
-
Hi Roman,
The example code above is to show the actual data scheme format being provided to the `upload_large()` (i.e., it should not be escaped/encoded). With this, could your try to update your code (and make sure that the buf contains the valid Base64 data as well):
From:
.upload_large("%22data%3Avideo/mp4%3B%20base64%22" + buf...)
To:
.upload_large('data:image/png;base64,' + buf...)
Please take a look and do let me know the error logs details if any.
Thanks.
Post is closed for comments.
Comments
3 comments