2017-07-27 45 views
0

enter image description here我上傳附件後上傳附件,我將其轉換爲base64,將其傳遞給我們的服務上傳附件在BOT模擬器。 我從路徑D:\ Images \ MobileRequest.PNG中選擇此附件,但是在將其上傳到BOT應用程序後,它顯示附件的路徑爲http://127.0.0.1:44185/v3/attachments/ne7djbemc9f40bifi/views/original/MobileRequest.PNG,因爲該路徑上沒有圖像,所以在將圖像轉換爲base64時,它會拋出一個錯誤,因爲「URI格式不被支持」。BOT模擬器中的附件上傳中的問題Bot框架

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

可能的複製[發送的圖像,而不是一個鏈接](https://stackoverflow.com/questions/39246174/發送圖像,而不是一個鏈接) –

回答

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 
} 
+0

那麼這是否意味着,我不會得到本地上傳的圖像? –

+0

上傳由頻道管理:如果您將圖片發佈到Slack,它將由Slack託管。 Skype或其他頻道也是如此,因此它爲您提供了一個可從機器人代碼獲取圖像的URL。 –

+0

好的。謝謝。 。 –