我正在使用电路JS软件开发工具包,并希望发送附加图像的消息。我在文档中发现,我应该将item.attachments设置为File[]对象。但是如果我只有图片地址(如https://abc.cde/fgh.png),我该如何做呢?
发布于 2019-11-15 21:42:44
为了能够在对话中发布图像,需要将图像上传到电路,这是在addTextItem API内部完成的,正如您已经了解到的那样。是的,这个API接受一个File对象数组。
您需要通过XMLHttpRequest将图像作为blob下载,然后构造一个文件对象。
const xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.open('GET',<url of image> , true);
xhr.onreadystatechange = async () => {
if (xhr.readyState == xhr.DONE) {
const file = new File([xhr.response], 'image.jpg', { lastModified: Date.now() });
const item = await client.addTextItem(convId.value, {
attachments: [file]
});
}
};
xhr.send();这是一个jsbin https://output.jsbin.com/sumarub
https://stackoverflow.com/questions/58872506
复制相似问题