【问题标题】:Issue in attachment upload in BOT emulator of Bot frameworkBot框架的BOT模拟器中附件上传问题
【发布时间】:2017-07-28 14:40:11
【问题描述】:

我正在 BOT 模拟器中上传附件,上传附件后我将其转换为 base64,以将其传递给我们的服务。 我从路径 D:\Images\MobileRequest.PNG 中选择了这个附件,但是在将其上传到 BOT 应用程序后,它显示附件的路径为http://127.0.0.1:44185/v3/attachments/ne7djbemc9f40bifi/views/original/MobileRequest.PNG,因为该路径上没有图像,因此在将图像转换为 base64 时,它会抛出一个错误,因为“不支持 URI 格式。”。

如何在 BOT 应用程序中获取实际物理路径,即“D:\Images\MobileRequest.PNG”。 以下是我的 BOT 应用程序中的代码

var dialog = new PromptDialog.PromptAttachment("Please attach screenshot ", "Sorry, I didn't get the attachment. Try again please.", 2);
context.Call(dialog, afterUpload);

private async Task afterUpload(IDialogContext context, IAwaitable<IEnumerable<Attachment>> result)
{
       IEnumerable<Attachment> attach = await result;
       string filePath = attach.FirstOrDefault().ContentUrl + "/" + attach.FirstOrDefault().Name;
       context.UserData.SetValue("filePath", filePath);  
}

string filePath =  string.Empty;
context.UserData.TryGetValue("filePath", out filePath);
using (System.Drawing.Image image = System.Drawing.Image.FromFile(filePath))
{
   using (MemoryStream m = new MemoryStream())
   {
     image.Save(m, image.RawFormat);
     byte[] imageBytes = m.ToArray();
     attach1 = Convert.ToBase64String(imageBytes);
   }
}

【问题讨论】:

标签: bots botframework


【解决方案1】:

您的机器人将被部署,因此您将无法访问本地文件。

您可以通过执行以下操作轻松转换位于 URL 的图像:

using (var client = new HttpClient())
{
     var bytes = await client.GetByteArrayAsync(imageUrl);
     var imageInBase64String = "image/jpeg;base64," + Convert.ToBase64String(bytes);

     // Do what you want with your converted image
}

【讨论】:

  • 那是否意味着,我不会得到本地上传的图片?
  • 上传由频道管理:如果您将图像发布到 Slack,它将由 Slack 托管。 Skype 或其他频道也是如此,因此它为您提供了一个 URL,您可以在其中从机器人的代码中获取图像。
  • 好的。谢谢你。 .
猜你喜欢
  • 1970-01-01
  • 2019-11-14
  • 1970-01-01
  • 1970-01-01
  • 2017-07-25
  • 1970-01-01
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多