当前位置: 首页 > news >正文

Blazor 附件上传和下载功能

效果图

@page "/uploadFile"
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment WebHostEnvironment
@inject ToastService ToastService
@inject DownloadService DownloadService<h3>UploadFile</h3><Button OnClick="@ButtonClick" Color="Color.Primary">弹窗上传组件</Button><Modal @ref="Modal" IsKeyboard="true"><ModalDialog Title="附件上传"><BodyTemplate><ButtonUpload TValue="string" ShowDownloadButton="true" OnDownload="OnDownload" IsSingle="true" IsMultiple="false" ShowProgress="true" OnChange="@OnClickToUpload"></ButtonUpload></BodyTemplate></ModalDialog>
</Modal>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using System.Net.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Web.Virtualization;
using Microsoft.JSInterop;
using BlazorApp;
using BlazorApp.Shared;
using BootstrapBlazor.Components;
using System.Diagnostics.CodeAnalysis;namespace BlazorApp.Pages
{public partial class UploadFile{[NotNull]private Modal? Modal { get; set; }private CancellationTokenSource? ReadToken { get; set; }//文件大小200MBprivate static long MaxFileLength => 200 * 1024 * 1024;private void ButtonClick(MouseEventArgs e){this.Modal.Toggle();}//下载private async Task OnDownload(BootstrapBlazor.Components.UploadFile item){try{//文件上传路径,在wwwroot路径下//item.PrevUrl这个是上传的时候复制的,只能用作临时使用,从数据库读取的是另一种实现方式String filePath = WebHostEnvironment.WebRootPath + item.PrevUrl.Replace("/", "\\");await using var stream = File.OpenRead(filePath);await DownloadService.DownloadFromStreamAsync(item.File.Name, stream);await ToastService.Success("下载成功");}catch (FileNotFoundException msg){await ToastService.Error("下载", msg.Message);}}//点击上传private async Task OnClickToUpload(BootstrapBlazor.Components.UploadFile file){await SaveToFile(file);}//上传private async Task<bool> SaveToFile(BootstrapBlazor.Components.UploadFile file){// Server Side 使用// Web Assembly 模式下必须使用 webapi 方式去保存文件到服务器或者数据库中// 生成写入文件名称var ret = false;//文件上传路径,在wwwroot路径下String filePath = WebHostEnvironment.WebRootPath + "\\uploadFiles";if (!Directory.Exists(filePath)){Directory.CreateDirectory(filePath);}//保存到服务器的名字var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.File.Name);var saveFilePath = Path.Combine(filePath, fileName);ReadToken ??= new CancellationTokenSource();//保存成功ret = await file.SaveToFileAsync(saveFilePath, MaxFileLength, ReadToken.Token);//把fileName到存数据库中,或者读取数据if (ret){//下载地址file.PrevUrl = "/uploadFiles/" + fileName;await ToastService.Success("上传文件成功");}else{var errorMessage = $"{"保存文件失败"} {file.OriginFileName}";file.Code = 1;file.Error = errorMessage;await ToastService.Error("上传文件", errorMessage);}return ret;}}
}

http://www.lryc.cn/news/229620.html

相关文章:

  • Git 安装配置
  • Center Smoothing Certified Robustness for Networks with Structured Outputs
  • C#几种截取字符串的方法
  • 【PG】PostgreSQL高可用方案repmgr部署(非常详细)
  • Linux Makefile配置问题
  • k8s篇之underlay网络和overlay区别
  • 掉瓶子小游戏
  • Elasticsearch7 入门 进阶
  • 你是怎么封装微信小程序的数据请求的?
  • C++ vector中capacity()和size() 的区别
  • 【Redis】redis-server和redis-cli
  • 【系统架构设计】架构核心知识: 2.4 系统建模过程和系统设计
  • 企业电子招标采购系统源码之从供应商管理到采购招投标、采购合同、采购执行的全过程数字化管理
  • ubantu libssl.so.1.1: cannot open shared object file
  • python matlplotlib/seaborn 绘制曲线的平均值标准差阴影图
  • 【Linux基础IO篇】深入理解文件系统、动静态库
  • flink 写入 starrocks 报错 too many filtered rows attachment
  • Windows 安装 Maven
  • 一文读懂关于IPv6的那些事
  • 数据结构—队列的实现
  • Linux_shell脚本中的stty
  • HTML转PDF模板
  • Clickhouse学习笔记(14)—— Clickhouse监控
  • Vue3 + Three.js + gltf-pipeline大型园区场景渲染与3D业务
  • 基于FPGA的PS端的Si5340的控制
  • 安装 Lua 的 HTTP 库
  • Redis解决缓存问题
  • 七个合法学习黑客技术的网站,让你从萌新成为大佬
  • 【数据结构】面试OJ题——带环链表(数学推论)
  • PostgreSQL中pg_ctl工具的使用