我想将文件从 NodeJs 发布到 PHP 服务器。
去做这个 :
0 - 我读了一个文件(一个 Power Point 文件)
1 - 我在 base64 中编码文件内容
2 - 我将文件发布到 PHP
3 - 我解码文件内容
4 - 我保存文件
但是当我在第 4 步之后打开文件时,文件已损坏。有人说为什么解码不起作用?
节点代码:
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) {