ok這都很簡單網路上一堆code
Html
<form enctype="multipart/form-data" method="post" asp-controller="FileStream" asp-action="Upload">
多選檔案:<input type="file" name="File" multiple /> <input type="submit" value="上傳" />
</form>
Controller
private IWebHostEnvironment _hostingEnvironment;
private readonly IFunction_Error _functionerror;
public FileStreamController(IWebHostEnvironment environment, IFunction_Error functionerror)
{
_hostingEnvironment = environment;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Upload(List<IFormFile> file)
{
string uploadfolder = Path.Combine(_hostingEnvironment.WebRootPath, "files");
foreach (IFormFile item in file)
{
if (item.Length > 0)
{
string filePath = Path.Combine(uploadfolder, item.FileName);
using (Stream fileStream = new FileStream(filePath, FileMode.Create))
{
await item.CopyToAsync(fileStream);
}
}
}
return View();
}
Ok很簡單吧User:我今天上傳了80MB的檔案出現了以下問題
好吧只好找解法了大概如下
serverOptions.Limits.MaxRequestBodySize = long.MaxValue; ...
services.Configure(options => { options.MultipartBodyLengthLimit = long.MaxValue; .... });
[DisableRequestSizeLimit]以上三種都沒用最後註解掉
看到一篇加入一個web.config程式碼如下
居然通了80MB上傳沒問題
一直以為.net core是沒web.conifg的
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483647" />
</requestFiltering>
</security>
</system.webServer>
</configuration>
User說:剛剛上傳了150MB的檔案都沒出現錯誤但檔案又沒傳上去發現List<IFormFile> file是null但後端是有收到
感覺是大小沒問題只不過設定上有些不正確
接著就將startup那段註解的加了回去終於不null了
services.Configure注意上面的MaxValue要修正阿不要真的那麼大還是要依專案來設定(options => { options.BufferBodyLengthLimit = long.MaxValue; options.KeyLengthLimit = int.MaxValue; options.MultipartBodyLengthLimit = long.MaxValue; options.MultipartBoundaryLengthLimit = int.MaxValue; options.ValueCountLimit = int.MaxValue; options.ValueLengthLimit = int.MaxValue; });
參考來源
.net core issues: 413 Request Entity Too Large nginx
Kestrel web server implementation in ASP.NET Core [资源分享].netcore 部署时遇到413 Request Entity Too Large 和 413Payload Too Large 的问题 asp.net core 3.0 解除文件上传大小限制,500.30、413、api-post-form方法拿不到参数 解决方案 How to increase file upload size in ASP.NET Core https://www.aspsnippets.com/Articles/ASPNet-Core-IFormFile-always-returns-NULL.aspx .Net Core IFormFile 始终为空的问题
