我的Web API服務:如何使用Microsoft Bot Framework將文件附加到郵件中?
[ActionName("download")]
[HttpGet]
public HttpResponseMessage Download()
{
var stream = new FileStream(HostingEnvironment.MapPath("~/tmp/") + "doc.pdf", FileMode.Open);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(stream)
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = document.Name + "." + document.AssociatedApplication.Extension
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
博特代碼:
if (message.Text.StartsWith("/d"))
{
var contentType = "application/pdf";
var attachment = new Attachment(contentType, "https://localhost/api/documents.download");
var response = await client.GetAsync("https://localhost/api/documents.download");
var data = await response.Content.ReadAsByteArrayAsync();
System.IO.File.WriteAllBytes(HostingEnvironment.MapPath("~/tmp/") + document.Name + "." + document.Extension, data);
var stream = System.IO.File.ReadAllBytes(HostingEnvironment.MapPath("~/tmp/") + document.Name + "." + document.Extension);
attachment.Content = stream;
var msg = message.CreateReplyMessage("This is your document: ");
msg.Attachments = new[] { attachment };
await context.PostAsync(msg);
}
如果我改變了服務器和客戶端「圖像/ PNG」上的內容類型,並從服務器發送PNG圖像然後客戶端這個示例工程完美 - 在Bot框架模擬器我得到了文本「這是你的文檔:」並收到圖像。
但是,如果我嘗試發送內容類型爲「application/pdf」或「application/octet-stream」的PDF文檔,並在內容類型爲「application/pdf」的客戶端上獲取它,那麼在Bot Framework Emulator上消息那樣:
這可能在談話中得到「真實」的文件,而不是鏈接下載(它如何與影像作品)?
PS:This question僅適用於「image/png」或類似的內容類型。