c# - 是否可以从内容 URI 获取确切的文件路径?

标签 c# botframework

我正在研究使用机器人框架创建请求的机器人。在那里我需要上传文件来支持票证。我在获取文件的确切文件路径时遇到问题。我能够获取内容 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/

相关文章:

c# - ASP.NET 请求扩展类型

c# - 包含相同参数的 ViewResult 和 ActionResult 出错

c# - 在 C# 中是否可以强制只能从构造函数调用私有(private)函数?

botframework - Bot Framework v4 - Bot 发起对话

botframework - Botframework模式通过电子邮件的常规简单正则表达式表达式给我错误

c# - 如何保留/存储我的 Azure 凭据?

c# - 使用 OpenGL 从 ffmpeg 帧渲染视频

javascript - MS Bot Framework 将值从服务器代码 (C#) 传递到前端代码 (JavaScript)

azure - 第二个 ConversationUpdate 事件将在用户第一次输入之后发生?

azure - 如何配置 Azure CI/CD 以更新最新更改