当前位置:网站首页>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>

 Insert picture description here
   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

原网站

版权声明
本文为[gc_ two thousand two hundred and ninety-nine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206262259232880.html