我在 Blazor 中有一个服务文件,可以将文件上传到我的项目,但我正在尝试检查上传的文件是否只有 jpg 及其姐妹文件。但是,我注意到我不能使用 ViewData 来显示条件确实满足的消息。
这是服务
public async Task Upload(IFileListEntry file)
{
var imgext = Path.GetExtension(file.Name);
string Message = "Plesae upload only jpg, png, gif or jpeg";
if (imgext == ".jpg" || imgext == ".png" || imgext == ".gif" || imgext == ".jpeg")
{
var path = Path.Combine(_oIWebHostEnvironment.ContentRootPath, "Images/UploadImages", file.Name);
var memoryStram = new MemoryStream();
await file.Data.CopyToAsync(memoryStram);
using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
memoryStram.WriteTo(fileStream);
}
}
else
{
ViewData Message; // viewdata is not available
}
**这是组件**
@code{
@*Upload Feature Image*@
IFileListEntry file;
async Task HandelFileSelcted(IFileListEntry[] files)
{
var imgext = Path.GetExtension(file.Name);
file = files.FirstOrDefault();
if (file != null)
{
await fileUpload.Upload(file);
}
}
}