翼度科技»论坛 编程开发 python 查看内容

学习ASP.NET Core Blazor编程系列十九——文件上传(下)

9

主题

9

帖子

27

积分

新手上路

Rank: 1

积分
27
学习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编程系列八——数据校验 学习ASP.NET Core Blazor编程系列十三——路由(完) 学习ASP.NET Core Blazor编程系列十五——查询  学习ASP.NET Core Blazor编程系列十六——排序           学习ASP.NET Core Blazor编程系列十七——文件上传(上)        学习ASP.NET Core Blazor编程系列十八——文件上传(中)         六、添加文件上传列表Blazor组件页面


  • 在Visual Studio 2022的解决方案资源管理器中,找到“Pages”文件夹,然后点击鼠标右键在弹出菜单中选择“添加-->新建文件夹”,然后把文件夹命名为“Descri”。如下图。  
 

    2. 在“Descri”文件夹上使用鼠标右键单击,在弹出菜单中选择“添加-->Razor组件…”,

     3.在弹出对话框中选择“Razor组件”,在名称输入框中输入“UpFileInfoList.razor”,然后点击“添加”按钮。如下图。


   4.UpFileInfoList这个页面用于显示已经上传的文件信息,这个页面的具体内容如下:

   
  1. @page "/Descri/UpFileInfoList"
  2. @using BlazorAppDemo.Models
  3. @using BlazorAppDemo.Utils
  4. @using Microsoft.EntityFrameworkCore
  5.  
  6. @inject IDbContextFactory<BookContext> dbFactory
  7.  
  8. <h3>已上传文件列表</h3>
  9. <table class="table" width="99%">
  10.     <thead>
  11.         <tr>
  12.             <th></th>
  13.             <th>
  14.                 @HtmlHelper.GetDisplayName(fileDesc,m=>m.Name)
  15.  
  16.             </th>
  17.             <th>
  18.                 @HtmlHelper.GetDisplayName(fileDesc ,m=> m.NewName)
  19.             </th>
  20.  
  21.             <th class="text-center">
  22.                 @HtmlHelper.GetDisplayName(fileDesc ,m=>m.UploadDateTime)
  23.             </th>
  24.             <th class="text-center">
  25.                 @HtmlHelper.GetDisplayName(fileDesc ,m=> m.FileSize)
  26.             </th>
  27.         </tr>
  28.     </thead>
  29.     <tbody>
  30.         @foreach (var item in fileDescs)
  31.         {
  32.             <tr>
  33.                 <td>
  34.                     <button id="delete" class="btn btn-primary" @onclick="@(e => DeleteFile(e, @item.ID))">删除</button>
  35.                 </td>
  36.                 <td>
  37.                     @item.Name
  38.  
  39.                 </td>
  40.                 <td>
  41.                     @item.NewName
  42.  
  43.                 </td>
  44.                 <td class="text-center">
  45.                     @item.UploadDateTime)
  46.                 </td>
  47.                 <td class="text-center">
  48.                     @item.FileSize
  49.                 </td>
  50.             </tr>
  51.         }
  52.     </tbody>
  53. </table>
  54.  
  55. @code {
  56.     private static BookContext _context;
  57.  
  58.     private List<FileDescribe> fileDescs = new List<FileDescribe>();
  59.     private FileDescribe fileDesc = new FileDescribe();
  60.     protected override async Task OnInitializedAsync()
  61.     {
  62.         _context = dbFactory.CreateDbContext();
  63.         fileDescs = _context.FileDescribe.ToList();
  64.         await base.OnInitializedAsync();
  65.     }
  66.     public void DeleteFile(MouseEventArgs e, int Id)
  67.     {
  68.         List<int> listId = new();
  69.         listId.Add(Id);
  70.         var entity = _context.Find<FileDescribe>(listId.ToArray());
  71.         _context.Remove<FileDescribe>(entity);
  72.        _context.SaveChangesAsync();
  73.     }
  74. }
  75.  
复制代码
 
  七、实现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”,然后使用鼠标左键点击“添加”按钮,创建一个新的类,代码如下 :

  1. using System.ComponentModel.DataAnnotations;
  2. using System.ComponentModel;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5.  
  6. namespace BlazorAppDemo.Utils
  7. {
  8.     public static class HtmlHelper
  9.     {
  10.         //an use the below extension method:
  11. public static string GetDisplayName<TModel, TProperty>(this TModel model, Expression<Func<TModel, TProperty>> expression)
  12.         {
  13.             Type type = typeof(TModel);
  14.             MemberExpression memberExpression = (MemberExpression)expression.Body;
  15.             string propertyName = ((memberExpression.Member is PropertyInfo)? memberExpression.Member.Name : null);
  16.  
  17.             DisplayAttribute attr;
  18.             attr = (DisplayAttribute)type.GetProperty(propertyName).GetCustomAttributes(typeof(DisplayAttribute), true).SingleOrDefault();
  19.  
  20.           
  21.             if (attr == null)
  22.             {
  23.                 MetadataTypeAttribute metadataType = (MetadataTypeAttribute)type.GetCustomAttributes(typeof(MetadataTypeAttribute), true).FirstOrDefault();
  24.                 if (metadataType != null)
  25.                 {
  26.                     var property = metadataType.MetadataClassType.GetProperty(propertyName);
  27.                     if (property != null)
  28.                     {
  29.                         attr = (DisplayAttribute)property.GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault();
  30.                     }
  31.                 }
  32.             }
  33.             return (attr != null) ? attr.Name : String.Empty;
  34.         }
  35.     }
  36.    
  37. }
  38.  
复制代码
3. 在Visual Studio 2022的菜单栏上,找到“调试-->开始调试”或是按F5键,Visual Studio 2022会生成BlazorAppDemo应用程序,并在浏览器中打开Home页面,我们使用鼠标点击左边的菜单栏上的“上传文件”菜单项,页面会进入“FileUpload1”页面,我们会看到我们写的图书列表页面,如下图。
 

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

 

 
 备注:虽然我们实现了上传文件信息的记录,但是现在还是存在一个数据刷新等小问题,等待解决。

 

   
来源:https://www.cnblogs.com/chillsrc/archive/2023/01/02/17019604.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具