简体   繁体   中英

How to write a base64 video to file in nodejs

My Express app is receiving a base64-encoded MP4 from the browser and writing it to a file. But the file isn't a valid video file, and the "file" utility simply identifies it as "data".

My Code:

const path = `${config.uploadPath}`;
const filename = `${uniqid()}.mp4`;

let base64Data = req.body.base64.replace(/^data:([A-Za-z-+/]+);base64,/, '');


fs.writeFileSync(`${path}${filename}`, base64Data, 'base64');

Are you sure there is a variable named base64 is request response? If so, please try this code:

req.body.base64 = req.body.base64.replace(/^data:(.*?);base64,/, ""); // <--- make it any type
req.body.base64 = req.body.base64.replace(/ /g, '+'); // <--- this is important

fs.writeFile(`${path}${filename}`, req.body.base64, 'base64', function(err) {
    console.log(err);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM