使用bot框架c#

时间:2017-05-17 18:53:25

标签: c# file-upload botframework

我目前正在尝试允许用户在对话流程中将文件上传到bot。从那里,机器人将获取文件并将其上传到blob存储。当文件进入时,content属性为null,但内容url,name和type都具有正确的值。

    public virtual async Task StackTraceGathered(IDialogContext context, IAwaitable<IMessageActivity> argument)
    {
        var message = await argument;
        FileName = message.Attachments[0].Name;
        HttpPostedFileBase file = (HttpPostedFileBase)message.Attachments[0].Content;

        string filePath = HttpContext.Current.Server.MapPath("~/Files/" + file.FileName);
        file.SaveAs(filePath);

        if (message.Attachments != null && message.Attachments.Any())
        {
            var attachment = message.Attachments.First();
            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("skype", StringComparison.InvariantCultureIgnoreCase) || message.ChannelId.Equals("msteams", StringComparison.InvariantCultureIgnoreCase))
                    && new Uri(attachment.ContentUrl).Host.EndsWith("skype.com"))
                {
                    var token = await new MicrosoftAppCredentials().GetTokenAsync();
                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                }

                var responseMessage = await httpClient.GetAsync(attachment.ContentUrl);

                var contentLenghtBytes = responseMessage.Content.Headers.ContentLength;

                await context.PostAsync($"Attachment of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received.");
            }
        }
        else
        {
            await context.PostAsync("Hi there! I'm a bot created to show you how I can receive message attachments, but no attachment was sent to me. Please, try again sending a new message including an attachment.");
        }
        PromptDialog.Text(context, ProblemStartDuration, "How long has this been an issue? (Provide answer in days, if issue has been occurring for less than one day put 1).");

        context.Wait(this.StackTraceGathered);
    }

1 个答案:

答案 0 :(得分:1)

我没有看到这个问题,但我猜你期待Content属性有所帮助。它不会,但你只需要Url。两种选择:

  • 下载机器人中的附件(作为您在问题中使用的代码)并上传到blob存储
  • 尝试使用类似StartCopyFromBlob(检查this
  • 直接从Url上传附件