【问题标题】:Base64 encode in nodejs and decode in php is different?Nodejs中的Base64编码和php中的解码不同吗?
【发布时间】:2018-07-13 08:40:06
【问题描述】:

我想将 en 文件从 NodeJs 发布到 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) {

【问题讨论】:

  • 最好为您的问题编写单独的答案,而不是将答案编辑到问题中。因为现在看起来这是一个未回答的问题(0 个答案)
  • 我想这样做,但我无法发布我的问题的答案。
  • 为什么不能?每个人都可以写答案,甚至是encouraged to answer your own question

标签: php node.js base64


【解决方案1】:

错误是我读取文件时的编码... 替换

fs.readFile(FilePath, 'utf8', function(err, data) {

fs.readFile(FilePath, function(err, data) {

谢谢大家

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    相关资源
    最近更新 更多