首页 文章

Bot框架的BOT仿真器中的附件上传问题

提问于
浏览
0

enter image description here
我正在上传附件后在BOT模拟器中上传附件我将其转换为base64,以将其传递给我们的服务 . 我从路径D:\ Images \ MobileRequest.PNG中选择此附件,但在将其上传到BOT应用程序后,它将显示附件的路径为http://127.0.0.1:44185/v3/attachments/ne7djbemc9f40bifi/views/original/MobileRequest.PNG,因为此路径上的图像不可用,因此在将图像转换为base64时,抛出错误"URI formats are not supported." .

如何在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);
   }
}

1 回答

  • 0

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

    您可以通过执行以下操作轻松转换位于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
    }
    

相关问题