当前位置:网站首页>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
边栏推荐
- [hybrid programming JNI] details of JNA in Chapter 11
- 运筹说 第66期|贝尔曼也有“演讲恐惧症”?
- LabVIEW Arduino TCP/IP远程智能家居系统(项目篇—5)
- Are there any risks for the top ten securities companies to register and open accounts? Is it safe?
- Système de distribution Unity Composants en tissu (y compris les dépendances d'appel dynamique)
- Release of dolphin scheduler video tutorial in Shangsi Valley
- VB. Net class library - 4 screen shots, clipping
- 买股票通过中金证券经理的开户二维码开户资金是否安全?想开户炒股
- [interface] pyqt5 and swing transformer for face recognition
- [710. random numbers in the blacklist]
猜你喜欢

UnityEditor编辑器扩展-表格功能

Briefly describe the model animation function of unity

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

树莓派初步使用
![Selenium电脑上怎么下载-Selenium下载和安装图文教程[超详细]](/img/ec/1c324dcf38d07742a139aac2bab02e.png)
Selenium电脑上怎么下载-Selenium下载和安装图文教程[超详细]

Yolov6: the fast and accurate target detection framework is open source

VB. Net class library (Advanced - 2 overload)

The sharp sword of API management -- eolink

xshell的安装、xftp的安装

Unity: 脚本缺失 “The referenced script (Unknown) on this Behaviour is missing!“
随机推荐
Electronic Society C language level 1 31. Calculate line segment length
【图像处理基础】基于matlab GUI图像曲线调整系统【含Matlab源码 1923期】
Detailed explanation of nmap parameters
The sharp sword of API management -- eolink
数据治理啥都干
Operator介紹
[mixed programming JNI] Part 7: JNI command lines
Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times
Are there any risks for the top ten securities companies to register and open accounts? Is it safe?
Unity 设置Material、Shader的方法
FPGA -vga display
[machine learning] - Introduction to vernacular and explanation of terms
Unity cloth system_ Cloth component (including dynamic call related)
xshell的安装、xftp的安装
Unity布料系統_Cloth組件(包含動態調用相關)
如何写好测试用例以及go单元测试工具testify简单介绍
UnityEditor編輯器擴展-錶格功能
从位图到布隆过滤器,C#实现
[mixed programming JNI] Part 9: JNI summary
【混合编程jni 】第七篇之JNI 的命令行们