当前位置:网站首页>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
边栏推荐
- Configuring assimp Library in QT environment (MinGW compiler)
- C语言:简单计算器多次使用代码实现
- 买股票通过中金证券经理的开户二维码开户资金是否安全?想开户炒股
- 打新债注册开户有没有什么风险?安全吗?
- WordPress collection plug-ins are recommended to be free collection plug-ins
- 电子协会 C语言 1级 30 、 等差数列末项计算
- Different subsequence problems I
- Operator介紹
- 不花一分钱做个在线的gif合成服务
- [710. random numbers in the blacklist]
猜你喜欢

Open world mecha games phantom Galaxy

A simple and crude method for exporting R language list to local
![[machine learning] - Introduction to vernacular and explanation of terms](/img/4c/e18fe52a71444c2ca08167ead9f28f.jpg)
[machine learning] - Introduction to vernacular and explanation of terms

From bitmap to bloom filter, C # implementation

【数学建模】基于matlab GUI随机节点的生成树【含Matlab源码 1919期】

数据治理啥都干

DLA model (classification model + improved segmentation model) + deformable convolution

CVPR 2022 - Interpretation of selected papers of meituan technical team

Using C to operate SQLSERVER database through SQL statement tutorial

Unity: the referenced script (unknown) on this behavior is missing“
随机推荐
如何写好测试用例以及go单元测试工具testify简单介绍
What are the test case design methods?
Redcap is ready to come out. It is indispensable to build a "meta universe"
Brief analysis of the self inspection contents of the blue team in the attack and defense drill
买股票通过中金证券经理的开户二维码开户资金是否安全?想开户炒股
[fundamentals of image processing] GUI image histogram equalization system based on MATLAB [including Matlab source code 1924]
Introduction de l'opérateur
[machine learning] - Introduction to vernacular and explanation of terms
JupyterLab 常用配置
How to download on selenium computer -selenium download and installation graphic tutorial [ultra detailed]
[mixed programming JNI] Part 6: operation of strings and arrays in native
CVPR 2022 - Interpretation of selected papers of meituan technical team
打新债注册开户有没有什么风险?安全吗?
vulnhub之dc8
Vulnhub's DC8
WordPress collection plug-ins are recommended to be free collection plug-ins
手机网上开户炒股安全吗 网上开户炒股安全吗
FPGA -VGA显示
Different subsequence problems I
How to write test cases and a brief introduction to go unit test tool testify