当前位置:网站首页>ASP.Net Core创建Razor页面上传多个文件(缓冲方式)(续)
ASP.Net Core创建Razor页面上传多个文件(缓冲方式)(续)
2022-06-29 19:37:00 【gc_2299】
继续学习微软的上传文件示例,学习支持目录浏览、删除文件、下载文件等功能,并将之迁移到之前创建的webapp项目中。
上述与文件操作相关的功能主要采用PhysicalFileProvider类实现,该类用于使用磁盘上的文件系统查找文件。其实也可以采用System.IO命名空间下的文件和文件夹相关类来获取文件信息,但没有PhysicalFileProvider用的方便,且后者可以以注册服务方式供项目中其它类调用。
通过调用PhysicalFileProvider类中的GetDirectoryContents函数获取指定目录下的文件和文件夹信息,并通过绑定模型方式将清单传递并显示在页面中。支持目录浏览、删除文件、下载文件等功能的后台代码如下所示(从微软示例程序中剥离的代码)
public IDirectoryContents PhysicalFiles {
get; private set; }
public async Task OnGetAsync()
{
PhysicalFiles = _fileProvider.GetDirectoryContents(string.Empty);
}
public IActionResult OnGetDownloadPhysical(string fileName)
{
var downloadFile = _fileProvider.GetFileInfo(fileName);
return PhysicalFile(downloadFile.PhysicalPath, MediaTypeNames.Application.Octet, fileName);
}
public IActionResult OnGetDeletehysical(string fileName)
{
if (string.IsNullOrEmpty(fileName))
{
return RedirectToPage("/Index");
}
IFileInfo RemoveFile = _fileProvider.GetFileInfo(fileName);
if (RemoveFile.Exists)
{
System.IO.File.Delete(RemoveFile.PhysicalPath);
}
PhysicalFiles = _fileProvider.GetDirectoryContents(string.Empty);
return Page();
}
前端页面中的代码也是将微软示例中不同页面中拼到一起的,本文测试可以正常运行。
<div>
<form enctype="multipart/form-data" method="post">
<dl>
<dt>
<label asp-for="MultiFiles"></label>
</dt>
<dd>
<input asp-for="MultiFiles" type="file" multiple accept=".pdf"/>
<span asp-validation-for="MultiFiles"></span>
</dd>
</dl>
<input asp-page-handler="Upload" class="btn" type="submit" value="上传文件" />
</form>
<p class="result">
@Model.Result
</p>
</div>
<div>
<h1>Files stored on disk</h1>
@if (Model.PhysicalFiles.Count() == 0)
{
<p>
No files are available. Visit one of the file upload scenario pages to upload one or more files.
</p>
}
else
{
<table>
<thead>
<tr>
<th></th>
<th>
Name
</th>
<th>
Size (bytes)
</th>
<th>
<code>PhysicalFileResult</code> from storage
</th>
</tr>
</thead>
<tbody>
@foreach (var file in Model.PhysicalFiles) {
<tr>
<td>
<a asp-page-handler="Deletehysical" asp-route-fileName="@file.Name">Delete</a>
</td>
<td>
<b>@file.Name</b><br>
</td>
<td class="text-center">
@file.Length.ToString("N0")
</td>
<td class="text-center">
<a asp-page-handler="DownloadPhysical" asp-route-fileName="@file.Name">Download</a>
</td>
</tr>
}
</tbody>
</table>
}
</div>
最后是程序运行效果,如下图所示:
参考文献:
[1]https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.extensions.fileproviders.physicalfileprovider?view=dotnet-plat-ext-6.0
[2]https://docs.microsoft.com/zh-cn/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0
边栏推荐
猜你喜欢

【️爬虫必备->Scrapy框架从黑铁到王者️】初篇——万字博文详解(建议收藏)

JVM(2) 垃圾回收

JVM(4) 字節碼技術+運行期優化

Win11安装权限在哪里设置?Win11安装权限设置的方法
MSYQL, redis, mongodb visual monitoring tool grafana

软件工程专业大二,之前的学习情况不太好该怎么规划后续发展路线

深度好文 | YOLOv5+DeepSORT多目标跟踪深入解读与测试(含源码)

Creators foundation highlights in June

创作者基金会 6 月份亮点

After CDN is added to the website, the Font Icon reports an error access control allow origin
随机推荐
洞见科技作为「唯一」隐私计算数商,「首批」入驻长三角数据要素流通服务平台
freeswitch拨打分机号
Qui vole dans un jeu d'écriture?
JVM(4) 字節碼技術+運行期優化
软件测试逻辑覆盖相关理解
WPS and Excelle
测试方法学习
Creators foundation highlights in June
After CDN is added to the website, the Font Icon reports an error access control allow origin
【剑指Offer】51. 数组中的逆序对
Win11系统频繁断网怎么办?Win11网络不稳定的解决方法
Game Maker 基金会呈献:归属之谷
数据基础设施升级窗口下,AI 新引擎的技术方法论
3-3主机发现-四层发现
One hour to build a sample scenario sound network to release lingfalcon Internet of things cloud platform
构建增强现实移动应用程序的六款顶级工具
Canonical的工程师们正努力解决Firefox Snap的性能问题
C#_摄像头图像转换为Bitmap格式及绘制十字线
k线图经典图解(收藏版)
雲服務器的安全設置常識