如何将图像转换为二进制数据并将其发送到xml-rpc服务器?我怎么打长途电话?
如果你正在使用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)