当前位置:网站首页>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
边栏推荐
- shell bash脚本注意:单行末尾转义符 \ 后千万不能有其他无关字符(多行命令)
- 测试方法学习
- Technical methodology of new AI engine under the data infrastructure upgrade window
- Kdd 2022 | prise en compte de l'alignement et de l'uniformité des représentations dans le Filtrage collaboratif
- 4-2端口Banner信息获取
- 终于,进亚马逊了~
- JVM(2) 垃圾回收
- Win11策略服务被禁用怎么办?Win11策略服务被禁用的解决方法
- 【笔记】再笔记--边干边学Verilog HDL – 014
- Canonical的工程师们正努力解决Firefox Snap的性能问题
猜你喜欢

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

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

细说GaussDB(DWS)复杂多样的资源负载管理手段
![[observation] softcom power liutianwen: embrace change and](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[observation] softcom power liutianwen: embrace change and "follow the trend" to become an "enabler" of China's digital economy

JVM (4) Bytecode Technology + Runtime Optimization

以其他组件为代价的性能提升不是好提升
![[boutique] detailed explanation of Pinia](/img/94/d332e32dba54be3c2d3f6ff08a85fa.png)
[boutique] detailed explanation of Pinia

Classic illustration of K-line diagram (Collection Edition)

One hour to build a sample scenario sound network to release lingfalcon Internet of things cloud platform

软件测试逻辑覆盖相关理解
随机推荐
Sophomore majoring in software engineering, the previous learning situation is not very good. How to plan the follow-up development route
KDD 2022 | 协同过滤中考虑表征对齐和均匀性
3-3主機發現-四層發現
[observation] softcom power liutianwen: embrace change and "follow the trend" to become an "enabler" of China's digital economy
虎符限币种提现 用户曲线出金即亏损
[sword finger offer] 51 Reverse pair in array
AI scene Storage Optimization: yunzhisheng supercomputing platform storage practice based on juicefs
Game maker Foundation presents: Valley of belonging
Win11策略服务被禁用怎么办?Win11策略服务被禁用的解决方法
细说GaussDB(DWS)复杂多样的资源负载管理手段
数据基础设施升级窗口下,AI 新引擎的技术方法论
KDD 2022 | characterization alignment and uniformity are considered in collaborative filtering
并查集(Union-Find)
La collection numérique Meng xiangshun, artiste national du tigre peint, est disponible en quantité limitée et est offerte avec Maotai de l'année du tigre
MBA-day26 数的概念与性质
Where is the win11 installation permission set? Win11 installation permission setting method
【Proteus仿真】矩阵键盘中断扫描
软件测试逻辑覆盖相关理解
凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
How important is it to make a silver K-line chart?