简体   繁体   English

NodeJs与xmlrpc服务器通信,node-xmlrpc库的base64编码,将下载的url图片编码为base64

[英]NodeJs communication with xmlrpc server and base64 encode for node-xmlrpc library, encode downloaded url images into base64

How can I convert images to binary data and send it to xml-rpc server?如何将图像转换为二进制数据并将其发送到 xml-rpc 服务器? How can I do a multiply call?我怎样才能进行多次调用?

If you are using node-xmlrpc library: For binary data you must use only Buffer.from(body), not .toString('base64)!如果您使用的是 node-xmlrpc 库:对于二进制数据,您必须仅使用 Buffer.from(body),而不是 .toString('base64)!

This is an example with downloaded url images and sending them to xml-rpc server.这是一个下载 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 });
      };
    },
  );
});

If you want a multiply call, you can do easily as:如果你想要一个乘法调用,你可以很容易地做到:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM