【问题标题】:NodeJs communication with xmlrpc server and base64 encode for node-xmlrpc library, encode downloaded url images into base64NodeJs 与 xmlrpc 服务器的通信和对 node-xmlrpc 库的 base64 编码,将下载的 url 图像编码为 base64
【发布时间】:2021-12-12 14:34:51
【问题描述】:

如何将图像转换为二进制数据并将其发送到 xml-rpc 服务器? 如何进行乘法呼叫?

【问题讨论】:

    标签: node.js xml base64 encode xml-rpc


    【解决方案1】:

    如果您使用的是 node-xmlrpc 库: 对于二进制数据,您只能使用 Buffer.from(body),而不是 .toString('base64)!

    这是一个下载 url 图片并将它们发送到 xml-rpc 服务器的示例。

    let base64data = [];
    let data = [];
    const binaryImage = require("request").defaults({ encoding: null });
    
    images.map(async (url_image) => {
      await binaryImage.get(
        `${url_image}`,
        async function (error, response, body) {
          if (!error && response.statusCode == 200) {
            url_image = Buffer.from(body);
            base64data.push(url_image);
            data.push({ photo: base64data, id: 0 });
          };
        },
      );
    });
    
    

    如果你想要一个乘法调用,你可以很容易地做到:

    setTimeout(() => {
      base64data.map((image, idx) => {
        setTimeout(() => {                           
            computeSessionId(sid, pw, key),          // if you need to compute session
            client.methodCall(
              "method",                              // method call
              [sid, params, { data: image, id: 0 }], // params, struct, where are images as buffer will automatically converted to base64 (binary)
              function (error, value) {
                if (error) {
                  console.log("res body:", error.body);
                } else {
                  console.log(value);
                }
              }
            );
        }, idx * 100);                               // + 100 each image need some times
      });
    }, 1000)
    

    【讨论】:

      猜你喜欢
      • 2012-03-07
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      相关资源
      最近更新 更多