I'm trying to create an ASP.NET Core MVC application that allows uploading files up to a fixed size and saving them on a dedicated physical location.
When I upload any file of a few megabytes (png images, jpg or other file types such as mp3 etc...), everything works correctly; however, when I try to upload a video of 80 megabytes, I get an error:
System.NullReferenceException: Object reference not set to an instance of an object.
postedFiles
was null.
TeachingController:
using Microsoft.AspNetCore.Mvc;
namespace VideoKnowledge.Controllers
{
public class TeachingController : Controller
{
private IWebHostEnvironment Environment;
public TeachingController(IWebHostEnvironment _environment)
{
Environment = _environment;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
[RequestFormLimits(MultipartBodyLengthLimit = 104857600)]
public IActionResult Index(List<IFormFile> postedFiles)
{
string wwwPath = this.Environment.WebRootPath;
string contentPath = this.Environment.ContentRootPath;
string path = Path.Combine(this.Environment.WebRootPath, "Uploads");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
List<string> uploadedFiles = new List<string>();
foreach (IFormFile postedFile in postedFiles)
{
string fileName = Path.GetFileName(postedFile.FileName);
using (FileStream stream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
{
postedFile.CopyTo(stream);
uploadedFiles.Add(fileName);
ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", fileName);
}
}
return View();
}
}
}
Views/Teaching/Index.cshtml
:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
body {
font-family: Arial;
font-size: 10pt;
}
</style>
</head>
<body>
<form method="post" enctype="multipart/form-data" asp-controller="Teaching" asp-action="Index">
<span>Select File:</span>
<input type="file" name="postedFiles" multiple />
<input type="submit" value="Upload" />
<br />
<span style="color:green">@Html.Raw(ViewBag.Message)</span>
</form>
</body>
</html>
web.config
:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="204857600" />
</requestFiltering>
</security>
</system.webServer>
</configuration>
program.cs
:
using Microsoft.AspNetCore;
using VideoKnowledge;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();
app.Run();
Could someone help me figure out what I'm doing wrong? The end result should be to load the video and show in the Teaching/index.cshtml
view a list of videos loaded like this (I have yet to write this).
Thanks in advance for your attention.