我正在研究使用机器人框架创建请求的机器人。在那里我需要上传文件来支持票证。我在获取文件的确切文件路径时遇到问题。我能够获取内容 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'。 ) 除了本地文件路径
最佳答案
没有用于检索文件在磁盘上的路径或用户上传文件的本地路径的机制(这被认为是安全风险: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}\"";
}
}
关于c# - 是否可以从内容 URI 获取确切的文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53593171/