dongyan7876 2018-07-13 08:40
浏览 327
已采纳

在nodejs中进行Base64编码并在php中进行解码是不同的?

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) {
  • 写回答

1条回答 默认 最新

  • doumu6941 2018-07-24 12:55
    关注

    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) {
    

    Thanks all

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作