当前位置:网站首页>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
边栏推荐
- 中金证券经理的开户链接开户买股票安全吗?有谁知道啊
- Partage de trois méthodes de sommation automatique dans un tableau Excel
- Common configuration of jupyterlab
- How to write test cases and a brief introduction to go unit test tool testify
- Unity3d plug-in anyportrait 2D bone animation
- Do an online GIF synthesis service at no cost
- Operator介紹
- Some ways out for older programmers
- Introduction to operator
- The sharp sword of API management -- eolink
猜你喜欢
[cloud native topic -51]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware redis microservice deployment process
WordPress collection plug-ins are recommended to be free collection plug-ins
论文解读(LG2AR)《Learning Graph Augmentations to Learn Graph Representations》
【老卫搞机】090期:键盘?主机?全功能键盘主机!
[Old Wei makes machines] issue 090: keyboard? host? Full function keyboard host!
UnityEditor編輯器擴展-錶格功能
Unity: 脚本缺失 “The referenced script (Unknown) on this Behaviour is missing!“
VB. Net class library to obtain the color under the mouse in the screen (Advanced - 3)
Unity4.6 Download
Flashtext, a data cleaning tool, has directly increased the efficiency by dozens of times
随机推荐
【LeetCode】1984. Minimum difference between highest and lowest of K scores
Electronic Society C language level 1 30, calculation of last term of arithmetic sequence
Operator介绍
Reading graph augmentations to learn graph representations (lg2ar)
[hybrid programming JNI] details of JNA in Chapter 11
Word chess based on heuristic search
[bug feedback] the problem of message sending time of webim online chat system
Fastadmin Aurora push send message registration_ Multiple IDs are invalid after being separated by commas
【BUG反馈】WebIM在线聊天系统发消息时间问题
【强基计划】数学与物理竞赛中的微积分部分视频
Unityeditor Editor Extension - table function
Electronic Society C language level 1 29, alignment output
不花一分钱做个在线的gif合成服务
FPGA -vga display
[mixed programming JNI] Part 9: JNI summary
阿里云服务器的购买、基本配置、(xshell)远程连接、搭建环境
VB. Net class library - 4 screen shots, clipping
Operator介紹
[strong foundation program] video of calculus in mathematics and Physics Competition
[cloud native topic -51]:kubesphere cloud Governance - operation - step by step deployment of microservice based business applications - database middleware redis microservice deployment process