当前位置:网站首页>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
边栏推荐
- Module externe unity3d anyportrait 2D Skeleton Animation
- VB. Net class library to obtain the color under the mouse in the screen (Advanced - 3)
- npm 命令提示Error: EACCES: permission denied
- BS-GX-016基于SSM实现教材管理系统
- Release of dolphin scheduler video tutorial in Shangsi Valley
- 买股票通过中金证券经理的开户二维码开户资金是否安全?想开户炒股
- 为什么我不推荐去SAP培训机构参加培训?
- xshell的安装、xftp的安装
- Operator介绍
- Why don't I recommend going to sap training institution for training?
猜你喜欢

WordPress collection plug-ins are recommended to be free collection plug-ins

在线协作文档综合评测 :Notion、FlowUs、Wolai、飞书、语雀、微软 Office、谷歌文档、金山文档、腾讯文档、石墨文档、Dropbox Paper、坚果云文档、百度网盘在线文档

Some ways out for older programmers

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

vulnhub之dc8

Reading graph augmentations to learn graph representations (lg2ar)

Configuring assimp Library in QT environment (MinGW compiler)
![[machine learning] - Introduction to vernacular and explanation of terms](/img/4c/e18fe52a71444c2ca08167ead9f28f.jpg)
[machine learning] - Introduction to vernacular and explanation of terms

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

Using C to operate SQLSERVER database through SQL statement tutorial
随机推荐
开放世界机甲游戏-Phantom Galaxies
微信小程序自动生成打卡海报
【强基计划】数学与物理竞赛中的微积分部分视频
浅谈分布式系统开发技术中的CAP定理
不同的子序列问题I
DLA model (classification model + improved segmentation model) + deformable convolution
6.24 learning content
How to write test cases and a brief introduction to go unit test tool testify
打新债注册开户有没有什么风险?安全吗?
Which platform is the safest for buying stocks and opening accounts? Ask for sharing
分享三種在Excel錶格中自動求和的方法
从位图到布隆过滤器,C#实现
[mathematical modeling] spanning tree based on Matlab GUI random nodes [including Matlab source code 1919]
VB. Net class library (advanced version - 1)
leetcode:6103. Delete the minimum score of the edge from the tree [DFS + connected component + value record of the subgraph]
数据治理啥都干
VB. Net class library - 4 screen shots, clipping
电子协会 C语言 1级 30 、 等差数列末项计算
[hybrid programming JNI] details of JNA in Chapter 11
Wechat applet automatically generates punch in Poster