六、添加文件上传列表Blazor组件页面

  1. 在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(); }
}  
七、实现Html.DisplayNameFor功能

在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编程系列十九——文件上传(下)的更多相关文章

  1. 学习ASP.NET Core Razor 编程系列十五——文件上传功能(三)

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  2. 学习ASP.NET Core Blazor编程系列十——路由(上)

    学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...

  3. 学习ASP.NET Core Razor 编程系列十四——文件上传功能(二)

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  4. 学习ASP.NET Core Blazor编程系列十——路由(中)

    学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...

  5. 学习ASP.NET Core Razor 编程系列十九——分页

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  6. 学习ASP.NET Core Razor 编程系列十八——并发解决方案

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  7. 学习ASP.NET Core Razor 编程系列十六——排序

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  8. 学习ASP.NET Core Blazor编程系列六——新增图书(上)

    学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...

  9. 学习ASP.NET Core Blazor编程系列八——数据校验

    学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...

  10. 学习ASP.NET Core Blazor编程系列九——服务器端校验

    学习ASP.NET Core Blazor编程系列一--综述 学习ASP.NET Core Blazor编程系列二--第一个Blazor应用程序(上) 学习ASP.NET Core Blazor编程系 ...

随机推荐

  1. POJ3311 Hie with the Pie(状压DP,Tsp)

    本题是经典的Tsp问题的变形,Tsp问题就是要求从起点出发经过每个节点一次再回到起点的距离最小值,本题的区别就是可以经过一个节点不止一次,那么先预处理出任意两点之间的最短距离就行了,因为再多走只会浪费 ...

  2. 分布式存储系统之Ceph集群RadosGW基础使用

    前文我们了解了MDS扩展相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/16759585.html:今天我们来聊一聊RadosGW的基础使用相关话题: ...

  3. 手写自定义springboot-starter,感受框架的魅力和原理

    一.前言 Springboot的自动配置原理,面试中经常问到,一直看也记不住,不如手写一个starter,加深一下记忆. 看了之后发现大部分的starter都是这个原理,实践才会记忆深刻. 核心思想: ...

  4. Dapr v1.9.0 版本已发布

    Dapr是一套开源.可移植的事件驱动型运行时,允许开发人员轻松立足云端与边缘位置运行弹性.微服务.无状态以及有状态等应用程序类型.Dapr能够确保开发人员专注于编写业务逻辑,而不必分神于解决分布式系统 ...

  5. v-for和router-link的共同使用

    1. 错误例子 <div style="color: red" v-for="item in pressionList" :key="item. ...

  6. 第三方库openPyxl读取excel文件

    import openpyxl from openpyxl.worksheet.worksheet import Worksheet def openpyxl_read(): #1.打开文件 work ...

  7. python信息检索实验之向量空间模型与布尔检索

    import numpy as np import pandas as pd import math def bool_retrieval(string): if string.count('and' ...

  8. ROSIntegration ROSIntegrationVision与虚幻引擎4(Unreal Engine 4)的配置

    ROSIntegration ROSIntegrationVision与虚幻引擎4(Unreal Engine 4)的配置 操作系统:Ubuntu 18.04 虚幻引擎:4.26.2 目录 ROSIn ...

  9. C#.NET实现二分查找

    二分搜索法 定义 二分法查找,也称为折半法,是一种在有序数组中查找特定元素的搜索算法. 适用范围 当数据量很大并且有序时,适宜采用该方法. 基本思想 假设数据是按升序排序的,对于给定值key,从序列的 ...

  10. HDLBits答案——Verification: Writing Testbenches

    1 clock module top_module ( ); reg clk; dut U1(.clk(clk)); initial begin clk = 0; end always begin # ...