当前位置:网站首页>ASP. Net core creates razor page and uploads multiple files (buffer mode) (Continued)
ASP. Net core creates razor page and uploads multiple files (buffer mode) (Continued)
2022-06-29 19:40:00 【gc_ two thousand two hundred and ninety-nine】
Continue to learn the Microsoft upload file example , Learning support directory browsing 、 Delete file 、 Download files and other functions , And migrate it to the previously created webapp In the project .
The above functions related to file operation mainly adopt PhysicalFileProvider Class implementation , This class is used to find files using the file system on the disk . In fact, it can also be used System.IO Namespace to get file information , But there is no PhysicalFileProvider Easy to use , And the latter can be called by other classes in the project in the form of registration service .
By calling PhysicalFileProvider Class GetDirectoryContents Function to get the file and folder information under the specified directory , The list is transferred and displayed in the page by binding the model . Support directory browsing 、 Delete file 、 The background code for downloading files and other functions is as follows ( Code stripped from Microsoft sample programs )
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();
}
The code in the front page is also a combination of different pages in Microsoft's example , This test can run normally .
<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=" Upload files " />
</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>
Finally, the program running effect , As shown in the figure below :
reference :
[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
边栏推荐
- 7. cancellation and closing
- Installation and configuration of MariaDB
- 3-2主机发现-三层发现
- php实现 提取不重复的整数(编程题目能够最快的熟悉函数)
- Luoqingqi: has high-end household appliances become a red sea? Casati took the lead in breaking the game
- 正则表达式系列之手机号码正则
- jfinal中如何使用过滤器监控Druid监听SQL执行?
- JVM (4) Bytecode Technology + Runtime Optimization
- JVM (3) class loading
- The era of data security solutions
猜你喜欢

Game Maker 基金会呈献:归属之谷

3 - 3 découverte de l'hôte - découverte à quatre niveaux

4-1端口扫描技术

There is no small green triangle on the method in idea

Creators foundation highlights in June

How important is it to make a silver K-line chart?

The list of winners in the classic Smurfs of childhood: bluedad's digital collection was announced

Rejected by a large company? Tencent experts summarized 11 reasons for being rejected!

揭秘!付费会员制下的那些小心机!

Common knowledge of ECS security settings
随机推荐
Installation and configuration of MariaDB
[proteus simulation] matrix keyboard interrupt scanning
揭秘!付费会员制下的那些小心机!
@SneakyThrows注解
剑指 Offer 41. 数据流中的中位数
使用 OpenCV 的基于标记的增强现实
KDD 2022 | 協同過濾中考慮錶征對齊和均勻性
STM32CubeMX 学习(6)外部中断实验
JVM(4) 字節碼技術+運行期優化
jfinal中如何使用过滤器监控Druid监听SQL执行?
Docker compose deploy the flask project and build the redis service
Sophomore majoring in software engineering, the previous learning situation is not very good. How to plan the follow-up development route
shell bash脚本注意:单行末尾转义符 \ 后千万不能有其他无关字符(多行命令)
powershell命令仅输出目录列表
终于,进亚马逊了~
1404万!四川省人社厅关系型数据库及中间件软件系统升级采购招标!
IP error problem of PHP laravel using AWS load balancer
[sword finger offer] 51 Reverse pair in array
小米笔试真题一
2022年理财利率都降了,那该如何选择理财产品?