我在主机器人类中的OnTurnAsync()
中有这个代码,让用户上传图像并将其保存在本地并且工作正常。但是,如果我想在不同的类(如MainDialog类)中执行此操作,我该怎么做?
更新:我设法通过以下代码执行此操作。但它接受了一切,我如何进行仅接受图像的验证?
更新:回答如下。
public class MainDialog : ComponentDialog
{
private const string InitialId = "mainDialog";
private const string TEXTPROMPT = "textPrompt";
private const string ATTACHPROMPT = "attachPrompt";
public MainDialog(string dialogId)
: base(dialogId)
{
WaterfallStep[] waterfallSteps = new WaterfallStep[]
{
FirstStepAsync,
SecondStepAsync,
};
AddDialog(new WaterfallDialog(InitialId, waterfallSteps));
AddDialog(new AttachmentPrompt(ATTACHPROMPT));
}
private static async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
return await stepContext.PromptAsync(
ATTACHPROMPT,
new PromptOptions
{
Prompt = MessageFactory.Text($"upload photo."),
RetryPrompt = MessageFactory.Text($"upload photo pls."),
});
}
private static async Task<DialogTurnResult> SecondStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
var activity = stepContext.Context.Activity;
if (activity.Attachments != null && activity.Attachments.Any())
{
foreach (var file in stepContext.Context.Activity.Attachments)
{
var remoteFileUrl = file.ContentUrl;
var localFileName = Path.Combine(Path.GetTempPath(), file.Name);
using (var webClient = new WebClient())
{
webClient.DownloadFile(remoteFileUrl, localFileName);
}
await stepContext.Context.SendActivityAsync($"Attachment {stepContext.Context.Activity.Attachments[0].Name} has been received and saved to {localFileName}.");
}
}
return await stepContext.EndDialogAsync();
}
使用此代码管理。
var activity = stepContext.Context.Activity;
if (activity.Attachments != null && activity.Attachments.Any())
{
foreach (var file in stepContext.Context.Activity.Attachments)
{
if (file.ContentType.EndsWith("/jpg") || file.ContentType.EndsWith("/png") || file.ContentType.EndsWith("/bmp") || file.ContentType.EndsWith("/jpe"))
{
var remoteFileUrl = file.ContentUrl;
var localFileName = Path.Combine(Path.GetTempPath(), file.Name);
using (var webClient = new WebClient())
{
webClient.DownloadFile(remoteFileUrl, localFileName);
}
await stepContext.Context.SendActivityAsync($"Attachment {stepContext.Context.Activity.Attachments[0].Name} has been received and saved to {localFileName}.");
return await stepContext.NextAsync();
}
else
{
await stepContext.Context.SendActivityAsync($"Attachment {file.Name} is not an image, it is {file.ContentType}.");
// restart the dialog from top
return await stepContext.ReplaceDialogAsync(InitialId);
}
}
}
就像Nicolas R说的那样,你可以做一些像这样的基本验证:
foreach (var file in activity.Attachments)
{
if (file.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
{
await turnContext.SendActivityAsync($"Attachment {file.Name} is an image of type {file.ContentType}.");
}
else
{
await turnContext.SendActivityAsync($"Attachment {file.Name} is not an image, it is {file.ContentType}.");
}
}
这当然允许任何具有以“image /”开头的内容类型的附件。您可能不希望允许任何以“image /”开头的内容类型,在这种情况下,您需要列出已接受的类型,如“image / png”和“image / jpeg”。您也可以考虑检查文件扩展名(看看它是“.png”还是“.jpg”等)