我想将en文件从nodejs发布到php服务器。
要执行此操作:
0-我读取了一个文件(电源点文件)
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) {