admin管理员组

文章数量:1291102

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');

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');
Share Improve this question asked Aug 21, 2018 at 18:26 Rubens FernandesRubens Fernandes 251 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

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);
});

本文标签: javascriptHow to write a base64 video to file in nodejsStack Overflow