[英]Base64 encode in nodejs and decode in php is different?
我想将NodeJs中的en文件发布到PHP服务器。
去做这个 :
0-我读取一个文件(一个Power Point文件)
1-我在base64中编码文件内容
2-我将文件发布到PHP
3-我解码文件内容
4-我保存文件
但是当我在步骤4之后打开文件时,文件已损坏。 有人说为什么解码不起作用?
nodejs代码:
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代码:
// set the POST content in $document
$document = base64_decode($document);
file_put_contents($fileName, $document);
编辑:错误是我读取文件时的编码...替换
fs.readFile(FilePath, 'utf8', function(err, data) {
至
fs.readFile(FilePath, function(err, data) {
错误是我读取文件时的编码...替换
fs.readFile(FilePath, 'utf8', function(err, data) {
至
fs.readFile(FilePath, function(err, data) {
谢谢大家
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.