我正在使用bot框架创建请求的bot。我需要上传文件以支持票证。我在获取文件的确切文件路径时遇到问题。我可以获取内容URI。如何获得确切的文件路径,例如"**C:\Users\j.jobin\Pictures\Mobile\images.jpeg**"
下面是我正在使用的代码
foreach(Attachment file in filesData) {
context.UserData.SetValue("Attachment", file.ContentUrl);
string FileURL = file.ContentUrl; // @"C:\Users\j.jobin\Pictures\Mobile\images.jpeg";
string fileName = file.Name;
string test = fileName;
//CreateSR(context);
string p = FileURL;
p = new Uri(p).LocalPath;
string TicketNo = "24712";
UploadAttchement(TicketNo, p, fileName);
}
内容URI看起来像 http://localhost:56057/v3/attachments/479e6660-f6ef-11e8-9659-d50246e856bf/views/original
我尝试使用string path = Path.GetFullPath(FileName);
,但这给了服务器路径(“ C:\ Program Files(x86)\ IIS Express \ tesla-cat.jpg”),而不是本地文件路径
答案 0 :(得分:1)
没有检索磁盘上文件路径或用户上载文件的本地路径的机制(这被认为存在安全风险:how-to-get-full-path-of-selected-file-on-change-of-input-type-file-using-jav)。
ContentUrl 属性为http://localhost:56057/v3/attachments/guid/views/original,因为该漫游器已连接到模拟器。此路径特定于Bot Framework Channel。您的本地仿真器的服务器托管在端口56057上。正如Simonare在评论中所述:您需要下载文件,并将其保存在某个位置。
此示例演示如何检索文件的字节:core-ReceiveAttachment
粗略地修改为在本地文件夹中保存多个文件:(在生产方案中,这不是一个好主意,如果没有其他保护措施。最好使用 Microsoft.WindowsAzure将字节上传到Blob存储中.Storage 或类似的东西。)
if (message.Attachments != null && message.Attachments.Any())
{
using (HttpClient httpClient = new HttpClient())
{
// Skype & MS Teams attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
if ((message.ChannelId.Equals(ChannelIds.Skype, StringComparison.InvariantCultureIgnoreCase)
|| message.ChannelId.Equals(ChannelIds.Msteams, StringComparison.InvariantCultureIgnoreCase)))
{
var token = await new MicrosoftAppCredentials().GetTokenAsync();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}
foreach (Attachment attachment in message.Attachments)
{
using (var responseMessage = await httpClient.GetAsync(attachment.ContentUrl))
{
using (var fileStream = await responseMessage.Content.ReadAsStreamAsync())
{
string path = Path.Combine(System.Web.HttpContext.Current.Request.MapPath("~\\Content\\Files"), attachment.Name);
using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write))
{
await fileStream.CopyToAsync(file);
file.Close();
}
}
var contentLenghtBytes = responseMessage.Content.Headers.ContentLength;
await context.PostAsync($"Attachment of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received.");
}
}
}
}
此BotBuilder-v4的示例提出了另一种方法:15.handling-attachments/AttachmentsBot.cs#L204
private static void HandleIncomingAttachment(IMessageActivity activity, IMessageActivity reply)
{
foreach (var file in activity.Attachments)
{
// Determine where the file is hosted.
var remoteFileUrl = file.ContentUrl;
// Save the attachment to the system temp directory.
var localFileName = Path.Combine(Path.GetTempPath(), file.Name);
// Download the actual attachment
using (var webClient = new WebClient())
{
webClient.DownloadFile(remoteFileUrl, localFileName);
}
reply.Text = $"Attachment \"{activity.Attachments[0].Name}\"" +
$" has been received and saved to \"{localFileName}\"";
}
}