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

C# 是否可以从内容URI中获取确切的文件路径?,c#,botframework,C#,Botframework,我正在使用bot框架来创建请求。在那里我需要上传文件来支持票。我在获取文件的确切文件路径时遇到问题。我能够获得内容URI。我如何获得像“**C:\Users\j.jobin\Pictures\Mobile\images.jpeg**”这样的确切文件路径 下面是我正在使用的代码 foreach(Attachment file in filesData) { context.UserData.SetValue("Attachment", file.ContentUrl); string Fi

我正在使用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看起来像


我尝试使用
stringpath=path.GetFullPath(文件名)但这提供了服务器路径('C:\Program Files(x86)\IIS Express\tesla cat.jpg'),而不是本地文件路径

没有检索磁盘上文件路径或用户上载文件的本地路径的机制(这被视为安全风险:)

ContentUrl属性是因为bot已连接到仿真器。此路径特定于Bot框架通道。本地仿真器的服务器托管在端口56057上。正如Simonare在评论中所说:您需要下载该文件,并将其保存在某个地方

此示例演示如何检索文件的字节:

粗略修改以将多个文件保存在本地文件夹中:(在生产场景中,如果没有其他保护措施,这不是一个好主意。最好使用Microsoft.WindowsAzure.storage或类似工具将字节上载到blob存储。)

此BotBuilder-v4示例提供了另一种方法:


可能的副本是不同的,如果文件是从服务器上传的,它可以通过字符串path=path.GetFullPath(FileName)获取当前目录路径;但在这里,当上传时,我需要获取文件路径并将其存储在某个地方。如果文件是从客户端上传到服务器的,则您正在使用字节数组发送文件。您需要将文件保存到服务器,然后从磁盘获取文件,或者最方便的方法是直接使用流。文件应作为流处理,因为如果bot在远程计算机上运行,路径将不同。频道应以流或附件的形式发送文件
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.");
            }
        }
    }
}
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}\"";
            }
        }