I want to post en file from NodeJs to PHP server.
To do this :
0 - I read a file (a Power Point file)
1 - I encode the file-content in base64
2 - I post the file to the PHP
3 - I decode the file content
4 - I save the file
But when I open the file after the step 4, the file is broken. Anyone say why the decode isn't working ?
nodejs code :
fs.readFile(FilePath, 'utf8', function(err, data) {
if (err) throw err;
request.post(
callback_url,
{ json: {
'document_id': id,
'document': Buffer.from(data).toString('base64'),
'content_type': mime.getType(resultFilePath + resultFile)
} },
function (error:any, response:any, body:any) {
console.log(body);
}
);
});
PHP code :
// set the POST content in $document
$document = base64_decode($document);
file_put_contents($fileName, $document);
Edit : The error was the encoding when I read the file ... replace
fs.readFile(FilePath, 'utf8', function(err, data) {
to
fs.readFile(FilePath, function(err, data) {