当前位置:网站首页>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
边栏推荐
- @SneakyThrows注解
- JVM (4) bytecode technology + runtime optimization
- freeswitch拨打分机号
- The era of data security solutions
- 誰在抖音文玩裏趁亂打劫?
- 3-3主機發現-四層發現
- npm ERR! fatal: early EOF npm ERR! fatal: index-pack failed
- Rejected by a large company? Tencent experts summarized 11 reasons for being rejected!
- docker compose 部署Flask项目并构建redis服务
- 细说GaussDB(DWS)复杂多样的资源负载管理手段
猜你喜欢

Shell bash script note: there must be no other irrelevant characters after the escape character \ at the end of a single line (multi line command)

Physical verification LVS process and Technology (Part I)

STM32CubeMX 学习(6)外部中断实验

使用 OpenCV 的基于标记的增强现实

How is the combination of convolution and transformer optimal?

Technical methodology of new AI engine under the data infrastructure upgrade window

@SneakyThrows注解

Connaissance générale des paramètres de sécurité du serveur Cloud

k线图经典图解(收藏版)

4-1端口扫描技术
随机推荐
凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
【历史上的今天】6 月 29 日:SGI 和 MIPS 合并;微软收购 PowerPoint 开发商;新闻集团出售 Myspace
[software testing] 01 -- software life cycle and software development model
技术保证质量,软件测试的这些测试方法你都掌握了吗?
终于,进亚马逊了~
【网络方向实训】-企业园区网络设计-【Had Done】
Creators foundation highlights in June
With these four security testing tools, software security testing can be said so easy!
Kdd 2022 | prise en compte de l'alignement et de l'uniformité des représentations dans le Filtrage collaboratif
Performance improvement at the cost of other components is not good
Go: how to write a correct UDP server
童年经典蓝精灵之百变蓝爸爸数字藏品中奖名单公布
Mba-day19 if P then q contradictory relation P and not Q
雲服務器的安全設置常識
Win11 system component cannot be opened? Win11 system widget cannot be opened solution
startService() 过程
乐鑫面试流程
4-1端口扫描技术
DAO 中存在的不足和优化方案
KDD 2022 | characterization alignment and uniformity are considered in collaborative filtering