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 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度