自适应卡-以字节为单位提供图像 [英] Adaptive cards - serving images in bytes
问题描述
我试图像这样将图像放入Bot框架中的Adaptive Card中:
I'm trying to put an image in Adaptive Card in Bot framework like this way:
card.Body.Add(new AdaptiveImage()
{
Type = "Image",
Url = new Uri(pictureUrl),
Size = AdaptiveImageSize.Large
});
正在工作.问题出在Url.我从外部Web服务以Base64格式获取图像.但是有时候我得到的图像太大,所以出现The uri string is too long
异常.
It's working. The problem is with Url. I get images from the external web service in Base64 format. But sometimes I get too large image so I get The uri string is too long
exception.
有什么办法可以解决这个问题?例如,启用以字节为单位将图像放入自适应卡中.
Is there any way how to handle that problem? For example, enable putting the image in Adaptive card in bytes.
推荐答案
感谢您报告此问题.根本原因是pictureUrl大于.NET的最大Uri长度.我们正在跟踪在此处解决此问题.
thanks for reporting this issue. The root cause is that the pictureUrl is longer than .NET's max Uri length. We're tracking fixing this here.
有一个非常简单的解决方法,因为该限制发生在.NET C#库中,您只是用来编写卡片,而WebChat并不使用C#库显示卡片(它使用JS库) ,而JS/HTML没有长度限制!).因此,在您的情况下唯一不起作用的是生成JSON ...,但是有一个简单的解决方法!
There's a pretty simple workaround available, since the limitation is occurring in the .NET C# library which you're using to simply author the card, but WebChat doesn't use the C# library to display cards (it uses the JS library, and JS/HTML doesn't have a length limit!). Therefore, the only thing that isn't working in your case is generating the JSON... but there's a simple fix!
解决方法:
定义以下类,扩展AdaptiveImage
,添加一个LongUrl
属性(该属性写入JSON中的同一url
属性).
Workaround:
Define the following class, extending AdaptiveImage
, adding a LongUrl
property (which writes to the same url
property in the JSON).
public class AdaptiveImageWithLongUrl : AdaptiveImage
{
[JsonProperty(PropertyName = "url", Required = Required.Always)]
public string LongUrl { get; set; }
}
然后,在分配长网址时,请使用新的图像类和新的属性!
Then, use your new image class and the new property when assigning long urls!
// A data URL that's longer than .NET max length
string actualUrl = "data:image/gif;base64," + string.Join("", new int[120000].Select(i => "A")) + "end";
AdaptiveCard card = new AdaptiveCard("1.0")
{
Body =
{
new AdaptiveImageWithLongUrl()
{
LongUrl = actualUrl
}
}
};
// Place the JObject in the attachment!
var attachment = new Attachment()
{
Content = card,
ContentType = "application/vnd.microsoft.card.adaptive",
Name = "cardName"
};
这篇关于自适应卡-以字节为单位提供图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!