【问题标题】:How to base64 encode inputs in React?如何在 React 中对输入进行 base64 编码?
【发布时间】:2017-11-11 12:51:05
【问题描述】:

我正在尝试使用 axios 发布两个输入,并且我想在发送它们之前对它们进行 base64 编码。

【问题讨论】:

    标签: reactjs post base64 axios


    【解决方案1】:

    自 v6 起已弃用

    const encodedString = new Buffer('your string here').toString('base64');

    改用

    const encodedString = Buffer.from('your string here').toString('base64');

    【讨论】:

    • 嘿,感谢您抽出宝贵的时间!在 ('your string here') 我可以输入 2 个变量,例如 (username, email) 吗?
    • 哎呀忘了问什么。我可以在上下文中看到它,以便我可以更简要地理解它吗?谢谢
    • encodedString = new Buffer(用户名 + ':' + 密码).toString('base64');。并且用户名和密码是字符串..
    • 成功了。谢谢
    • @vinayverma Tanks 为您解答。你能告诉我如何在服务器端解码这些内容吗?我使用 .net 作为我的后端
    【解决方案2】:

    最好使用 Buffer.from('Your String').toString('base64'),因为新的 Buffer 已被弃用

    【讨论】:

      【解决方案3】:

      考虑使用与btoaatob 兼容的base-64,在反应和本机反应中为我工作:

      npm install base-64 --save
      
      import {decode as base64_decode, encode as base64_encode} from 'base-64';
      let encoded = base64_encode('YOUR_DECODED_STRING');
      let decoded = base64_decode('YOUR_ENCODED_STRING');
      

      如果您使用 typescript,请不要忘记安装类型以及 npm i --save-dev @types/base-64 作为开发依赖项。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-26
        • 2011-09-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多