学习ASP.NET Core Blazor编程系列十九——文件上传(下)
六、添加文件上传列表Blazor组件页面
- 在Visual Studio 2022的解决方案资源管理器中,找到“Pages”文件夹,然后点击鼠标右键在弹出菜单中选择“添加-->新建文件夹”,然后把文件夹命名为“Descri”。如下图。

2. 在“Descri”文件夹上使用鼠标右键单击,在弹出菜单中选择“添加-->Razor组件…”,
3.在弹出对话框中选择“Razor组件”,在名称输入框中输入“UpFileInfoList.razor”,然后点击“添加”按钮。如下图。

4.UpFileInfoList这个页面用于显示已经上传的文件信息,这个页面的具体内容如下:
@page "/Descri/UpFileInfoList"
@using BlazorAppDemo.Models
@using BlazorAppDemo.Utils
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<BookContext> dbFactory <h3>已上传文件列表</h3> <table class="table" width="99%"> <thead> <tr>
<th></th>
<th>
@HtmlHelper.GetDisplayName(fileDesc,m=>m.Name)
</th>
<th>
@HtmlHelper.GetDisplayName(fileDesc ,m=> m.NewName)
</th>
<th class="text-center">
@HtmlHelper.GetDisplayName(fileDesc ,m=>m.UploadDateTime)
</th>
<th class="text-center">
@HtmlHelper.GetDisplayName(fileDesc ,m=> m.FileSize)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in fileDescs)
{
<tr>
<td> <button id="delete" class="btn btn-primary" @onclick="@(e => DeleteFile(e, @item.ID))">删除</button> </td>
<td>
@item.Name
</td>
<td>
@item.NewName
</td>
<td class="text-center">
@item.UploadDateTime)
</td>
<td class="text-center">
@item.FileSize
</td>
</tr>
}
</tbody>
</table>
@code {
private static BookContext _context;
private List<FileDescribe> fileDescs = new List<FileDescribe>();
private FileDescribe fileDesc = new FileDescribe();
protected override async Task OnInitializedAsync()
{ _context = dbFactory.CreateDbContext();
fileDescs = _context.FileDescribe.ToList();
await base.OnInitializedAsync();
} public void DeleteFile(MouseEventArgs e, int Id)
{ List<int> listId = new();
listId.Add(Id); var entity = _context.Find<FileDescribe>(listId.ToArray());
_context.Remove<FileDescribe>(entity);
_context.SaveChangesAsync(); }
}
在ASP.NET CORE MVC中有一个非常有用的类Html,其中有一个方法DisplayNameFor(m=>m.Name),根据实体类中属性上的特性Display所描述的信息,在页面上显示。在Blazor中默认没有这个功能,需要我们自己来实现。
1. 如第六点中的代码,我们使用@HtmlHelper.GetDisplayName方法来显示每个类属性的名称。 FileDescribe实体类中的 Display 特性提供这属性需要在页面上的显示值。 例如,Name属性通过特性[Display(Name = "文件名称")]进行设置,因此呈现窗体时会显示“文件名称”。如下图。

2.接下来我们来实现这个辅助类,在Visual Studio 2022的解决方案资源管理器中,选中“Utils”文件夹,单击鼠标右键,在弹出的快捷菜单中选择“添加-->类”,在弹出的“添加新项”对话框的名称输入框中,输入“HtmlHelper”,然后使用鼠标左键点击“添加”按钮,创建一个新的类,代码如下 :
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;
namespace BlazorAppDemo.Utils
{
public static class HtmlHelper
{
//an use the below extension method: public static string GetDisplayName<TModel, TProperty>(this TModel model, Expression<Func<TModel, TProperty>> expression)
{
Type type = typeof(TModel); MemberExpression memberExpression = (MemberExpression)expression.Body;
string propertyName = ((memberExpression.Member is PropertyInfo)? memberExpression.Member.Name : null);
DisplayAttribute attr;
attr = (DisplayAttribute)type.GetProperty(propertyName).GetCustomAttributes(typeof(DisplayAttribute), true).SingleOrDefault();
if (attr == null)
{ MetadataTypeAttribute metadataType = (MetadataTypeAttribute)type.GetCustomAttributes(typeof(MetadataTypeAttribute), true).FirstOrDefault();
if (metadataType != null)
{
var property = metadataType.MetadataClassType.GetProperty(propertyName);
if (property != null)
{ attr = (DisplayAttribute)property.GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault();
}
}
}
return (attr != null) ? attr.Name : String.Empty;
}
}
}
3. 在Visual Studio 2022的菜单栏上,找到“调试-->开始调试”或是按F5键,Visual Studio 2022会生成BlazorAppDemo应用程序,并在浏览器中打开Home页面,我们使用鼠标点击左边的菜单栏上的“上传文件”菜单项,页面会进入“FileUpload1”页面,我们会看到我们写的图书列表页面,如下图。

5. 我们在“多文件上传示例”页面中选择一个上传文件,然后应用程序会自动上传文件,并会在数据库中记录了一上传文件的相关信息,并会在页面中显示一个已经上传的文件列表。如下图。

备注:虽然我们实现了上传文件信息的记录,但是现在还是存在一个数据刷新等小问题,等待解决。
学习ASP.NET Core Blazor编程系列十九——文件上传(下)的更多相关文章
- 学习ASP.NET Core Razor 编程系列十五——文件上传功能(三)
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- 学习ASP.NET Core Blazor编程系列十——路由(上)
学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...
- 学习ASP.NET Core Razor 编程系列十四——文件上传功能(二)
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- 学习ASP.NET Core Blazor编程系列十——路由(中)
学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...
- 学习ASP.NET Core Razor 编程系列十九——分页
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- 学习ASP.NET Core Razor 编程系列十八——并发解决方案
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- 学习ASP.NET Core Razor 编程系列十六——排序
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- 学习ASP.NET Core Blazor编程系列六——新增图书(上)
学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...
- 学习ASP.NET Core Blazor编程系列八——数据校验
学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...
- 学习ASP.NET Core Blazor编程系列九——服务器端校验
学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...
随机推荐
- 离线安装chrome浏览器的postman插件
最近开始研究webapi相关的东西,看到chrome浏览器的有个postman插件挺好用的,但是安装包下载下来以后会出现这种情况,这时候我们可以把crx后缀的改成zip格式的然后解压,然后选择开发者模 ...
- EF在二手市场中的使用
二手市场这个小项目是我第一次用EF,边学边写边记录吧 首先明确几个知识点 存储过程 存储过程简单来说,就是为以后的使用而保存的一条或多条SQL语句的集合.可将其视为批件,虽然它们的作用不仅限于批处理. ...
- SpringBoot(一) - SpringBoot 初识
1.创建SpringBoot项目 1.1 使用Spring Initializr 的 Web页面创建项目 创建网址:https://start.spring.io/ 1.2 使用IDEA创建 省略: ...
- NVIDIA Isaac Gym安装与使用
NVIDIA做的Isaac Gym,个人理解就是一个类似于openai的Gym,不过把环境的模拟这个部分扔到了GPU上进行,这样可以提升RL训练的速度. 官网:https://developer.nv ...
- 齐博x1标签之无刷新显示更多
示范代码如下: <div class="ListMoreInfos"> {qb:tag name="news_list_page_listdata02&quo ...
- python用ffmpeg进行视频处理
1.下载及安装 在ffmpeg官网https://ffmpeg.zeranoe.com/builds/可以下载到需要的版本,然后解压到D盘,添加环境变量(如D:\ffmpeg\bin) 在cmd输入f ...
- IP分类与子网划分
1.IP地址的格式 每一类地址都由两个固定长度的字段组成: (1)网络号 net-id:它标志主机(或路由器)所连接到的网络 (2)主机号 host-id:它标志该主机(或路由器). 最大可指派 ...
- mlflow详细安装部署
1.安装docker # 安装工具 sudo yum install -y yum-utils # 添加yum仓库配置 sudo yum-config-manager --add-repo https ...
- Vue3组件间传值
12种方式 1. 父组件 ./father.vue 点击查看代码 <template> <h1>father:</h1> <h3>子组件传过来的:{{ ...
- go工具pprof部署
在做内存分析时,用到了pprof,这里做一下部署介绍和入门级别的使用. pprof是golang的性能工具,有两种交互方式:命令行交互和web交互,同时还支持性能分析数据的图形化展示. 部署pprof ...