0

enter image description hereI am uploading an attachment in BOT emulator, after uploading an attachment I am converting it to base64, to pass it to our service. I pick this attachment from path D:\Images\MobileRequest.PNG, but after uploading it to BOT app it shows the path of attachment as http://127.0.0.1:44185/v3/attachments/ne7djbemc9f40bifi/views/original/MobileRequest.PNG, as the image is not available on this path, So while converting the image to base64, it throws an error as "URI formats are not supported.".

How to get actual physical path i.e "D:\Images\MobileRequest.PNG" in BOT app. Below is code from my BOT app

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);
   }
}
Amol Pawar
  • 251
  • 1
  • 15
  • Possible duplicate of [Send an image rather than a link](https://stackoverflow.com/questions/39246174/send-an-image-rather-than-a-link) – Ezequiel Jadib Jul 27 '17 at 14:59

1 Answers1

0

Your bot will be deployed so you will not have access to local files.

You can easily convert your image located at a URL by doing the following:

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
}
Nicolas R
  • 13,812
  • 2
  • 28
  • 57