当前位置:网站首页>ASP. Net core create MVC project upload file (buffer mode)
ASP. Net core create MVC project upload file (buffer mode)
2022-06-26 23:13:00 【gc_ two thousand two hundred and ninety-nine】
Learned common webapp Upload file in , Look again from MVC Upload files to physical folders through buffering in the project . The main difference between the two is webapp Pass through model binding IFormFile object , and mvc Through the controller and action Pass on IFormFile object , Follow up on IFromFile Object processing is almost the same .
Run the following command to create mvc project .
dotnet new mvc -o UploadFileByMVC
code -r UploadFileByMVC
Directly in the default HomeController Class to upload multiple files and a single file , The code refers to references 1-2.
[HttpPost]
public async Task<IActionResult> UploadMultiFile(List<IFormFile> files)
{
long size = files.Sum(f => f.Length);
foreach (var formFile in files)
{
var filePath = Path.Combine(_targetFilePath, formFile.FileName);
if (formFile.Length > 0)
{
using (var stream = new FileStream(filePath, FileMode.Create))
{
await formFile.CopyToAsync(stream);
}
}
}
return Ok(new {
count = files.Count, size });
}
[HttpPost]
public async Task<IActionResult> UploadSingleFile(IFormFile file)
{
var filePath = Path.Combine(_targetFilePath, file.FileName);
if (file.Length > 0)
{
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
}
return Ok(new {
count = 1 ,file.Length });
}
A page is a mix of forms that upload a single file and multiple files , The code and screenshot are as follows :
<p> Multiple file upload :</p>
<form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="UploadMultiFile">
<div>
<input type="file" name="files" multiple />
</div>
<div>
<input type="submit" value="Upload" />
</div>
</form>
<p> Single file upload :</p>
<form method="post" enctype="multipart/form-data" asp-controller="Home" asp-action="UploadSingleFile">
<div>
<input type="file" name="file" />
</div>
<div>
<input type="submit" value="Upload" />
</div>
</form>

The program is relatively simple , It works , The Microsoft example also includes the ability to list the files in the buffer folder , This code will be stripped and put into the project of this article later .
reference :
[1]https://blog.csdn.net/tie123000/article/details/96868206
[2]https://blog.csdn.net/weixin_41960204/article/details/107575845?spm=1001.2101.3001.6650.14&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-14-107575845-blog-96868206.pc_relevant_paycolumn_v3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-14-107575845-blog-96868206.pc_relevant_paycolumn_v3
边栏推荐
- 从位图到布隆过滤器,C#实现
- DLA model (classification model + improved segmentation model) + deformable convolution
- Common configuration of jupyterlab
- Extensions de l'éditeur d'unityeditor - fonctions de table
- 运筹说 第66期|贝尔曼也有“演讲恐惧症”?
- Raspberry pie preliminary use
- Reading graph augmentations to learn graph representations (lg2ar)
- Which securities company is the most convenient, safe and reliable for opening an account
- Bs-gx-016 implementation of textbook management system based on SSM
- 美术向的Unity动画知识
猜你喜欢

DAST 黑盒漏洞扫描器 第五篇:漏洞扫描引擎与服务能力

微信小程序自动生成打卡海报

Data governance does everything

Redcap is ready to come out. It is indispensable to build a "meta universe"

从位图到布隆过滤器,C#实现

Restfultoolkitx of idea utility plug-in -- restful interface debugging

Unity4.6 Download

CVPR 2022 - Interpretation of selected papers of meituan technical team

Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times

Unityeditor Editor Extension - table function
随机推荐
Vulnhub's DC8
买股票通过中金证券经理的开户二维码开户资金是否安全?想开户炒股
6.24 learning content
UnityEditor編輯器擴展-錶格功能
Unity 设置Material、Shader的方法
L'outil de nettoyage des données flashtext améliore directement l'efficacité de plusieurs dizaines de fois
从位图到布隆过滤器,C#实现
【老卫搞机】090期:键盘?主机?全功能键盘主机!
Système de distribution Unity Composants en tissu (y compris les dépendances d'appel dynamique)
What are the test case design methods?
Open world mecha games phantom Galaxy
Unity布料系统_Cloth组件(包含动态调用相关)
树莓派初步使用
【数学建模】基于matlab GUI随机节点的生成树【含Matlab源码 1919期】
FPGA -vga display
Wechat applet automatically generates punch in Poster
Selenium电脑上怎么下载-Selenium下载和安装图文教程[超详细]
Brief analysis of the self inspection contents of the blue team in the attack and defense drill
Partage de trois méthodes de sommation automatique dans un tableau Excel
Configuring assimp Library in QT environment (MinGW compiler)