Mohammad
Mohammad

Reputation: 45

Error in Upload large file in asp.net core 6 (HTTP Error 413.1 - Request Entity Too Large)

I created a project with ASP.NET Core 6 MVC web app.

I want to upload a large file, but I get HTTP Error 413.1 - Request Entity Too Large.

Client code is:

<form method="post" enctype="multipart/form-data">
    <div class="box-body">
        <div class="form-group">
            <label for="exampleInputFile">Sending File</label>
            <input type="file" name="uploadfile" multiple>
        </div>
        <div class="box-footer">
            <button type="submit" class="btn btn-info pull-right">Upload</button>
        </div>
    </div>
</form>

And server code is:

    //100MG
    //[RequestSizeLimit(102428800)]
    [RequestSizeLimit(52428800)]
    [HttpPost]
    public IActionResult UploadFile(IFormFile uploadfile, [FromServices] IWebHostEnvironment env)
    {
        var filePath = Path.Combine(env.ContentRootPath, "Film\\Csharp", uploadfile.FileName);

        using (var stream = System.IO.File.Create(filePath))
        {
            uploadfile.CopyTo(stream);
        }

        return RedirectToAction("UploadFile");
    }

When I read the Microsoft document, it said, the following entries were added to the web.config file, but I could not find the web.config file. Can anyone help me?

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="52428800" />
    </requestFiltering>
  </security>
</system.webServer>

Upvotes: 2

Views: 1665

Answers (1)

Xinran Shen
Xinran Shen

Reputation: 9953

Asp.Net Core projects are created without a web.config file by default, But you can add it by yourself.

enter image description here

After you create a web.config file, you can add code to configure the size of upload file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="xxxxxxxxxx" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

More details you can refer to this Docs.

Upvotes: 3

Related Questions