asp.net core 上传大文件配置 步骤

1. 在 startup 文件中配置

 public void ConfigureServices(IServiceCollection services)
        { services.Configure<FormOptions>(x =>
            {
                x.ValueLengthLimit = int.MaxValue;
                x.MultipartBodyLengthLimit = int.MaxValue;  //最大值
                x.MemoryBufferThreshold = int.MaxValue;
            });
        }

2. 在控制其action 中添加    属性 [DisableRequestSizeLimit]

 1  [DisableRequestSizeLimit]
 2         public async Task<IActionResult> SaveVideo(List<IFormFile> files, int id)
 3         {
 4              
 5             long size = files.Sum(f => f.Length);
 6             
 7             string contentRootPath = _webHostEnvironment.ContentRootPath;
 8             foreach (var formFile in files)
 9             {
10                 if (formFile.Length > 0)
11                 {
12 
13                     string fileExt = Path.GetExtension(formFile.FileName); //文件扩展名,不含“.”
14                     if (!VideoExt.Contains(fileExt))
15                     {
16                         return Json(new { msg = "only mp4 avi wmv file allowed upload " + fileExt, count = 0 });
17 
18                     }
19 
20                     long fileSize = formFile.Length; //获得文件大小,以字节为单位
21                     if (fileSize / 1024 / 1024 >160)
22                     {
23                         return Json(new
24                         {
25                             msg = " video filesize must be less than 160MB ",
26                             count = 0
27                         });
28                     }
29                     //string newFileName = System.Guid.NewGuid().ToString() + "." + fileExt; //随机生成新的文件名
30                     //string newFileName = id + fileExt; //随机生成新的文件名
31                     string path = contentRootPath + "/Up/" + id + "/Video/";
32 
33                     DirectoryInfo di = new DirectoryInfo(path);
34 
35                     if (!di.Exists) { di.Create(); }
36 
37                     var filePath = path + formFile.FileName;
38                     using (var stream = new FileStream(filePath, FileMode.Create))
39                     {
40 
41                         await formFile.CopyToAsync(stream);
42                     }
43                 }
44             }
45 
46             return Ok(new { count = files.Count, size });
47         }

3. 在web.config  <system.webServer> 节点下  文件中添加  以下代码

         <security>
         <requestFiltering>
           <requestLimits maxAllowedContentLength="419430400" />
        </requestFiltering>
       </security>
 
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <configuration>
 3   <location path="." inheritInChildApplications="false">
 4     <system.webServer>
 5       <handlers>
 6         <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
 7       </handlers>
 8       <aspNetCore processPath=".\会务通知生成.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
 9         <security>
10         <requestFiltering>
11            <requestLimits maxAllowedContentLength="419430400" />
12         </requestFiltering>
13     </security>
14     </system.webServer>
15   </location>
16 </configuration>

 

posted on 2021-12-08 13:46  码农at突泉  阅读(509)  评论(0)    收藏  举报