我有 Web API 服务:
[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);
}
如果我将服务器和客户端上的内容类型更改为“image/png”并将 PNG 图像从服务器发送到客户端,那么此示例工作完美 - 在 Bot Framework Emulator 中,我收到文本“这是您的文档:”并收到图像。
但是,如果我尝试发送内容类型为“application/pdf”或“application/octet-stream”的 PDF 文档并在内容类型为“application/pdf”的客户端上获取它,那么在 Bot Framework Emulator 上我会收到这样的消息:
这是否可以进入对话“真实”文档而不是下载链接(它如何与图像一起使用)?
PS:此问题仅适用于“image/png”或类似内容类型。