skip to Main Content

i have been looking for a way to upload a large files to my server.
My form works for small files x<30MB.
This is my form:

    <div class="card">
     <div class="card-header">
         <center><b>Dodaj obrazek</b></center>
      </div>
<div class="card-body">
<form method="post" enctype="multipart/form-data">
 <div class="mb-3">
     <input type="file" asp-for="Gallery.Upload" class="form-control"  />
 </div>
 <button class="btn btn-success">Upload</button>
    </form>
    </div>
</div>

I am using IFormFile

 [NotMapped]
    public IFormFile Upload { get; set; }

I have been trying many scenarios like declaring RequestFormLimit before SiteModel or before Method:

 [RequestFormLimits(MultipartBodyLengthLimit = 104857600)]
public class SiteModel : PageModel

    [RequestFormLimits(MultipartBodyLengthLimit = 104857600)]
    public async Task OnGetAsync()

but still got nothing.

//UPDATE
I have added this to my Program.cs

    builder.Services.Configure<FormOptions>(conf =>
{
    conf.ValueLengthLimit = int.MaxValue;
    conf.MultipartBodyLengthLimit = int.MaxValue;
    conf.MemoryBufferThreshold = int.MaxValue;
});

and this before SiteModel

    [DisableRequestSizeLimit]

and now i can upload only up to 100 MB, but i want to make it larger, like 4GB limit per one upload..

How can i change limit size?

3

Answers


  1. After updating an older app to .net6 we ran into the same issue, and also tried all the things you have tried. It wasn’t until a web.config was added back to the project that allowed the large upload to succeed. Here is all that was required:

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

    Hope it helps.

    Login or Signup to reply.
  2. For.Net Core 6 and Razor Pages, we have some tips in Microsoft documentation (https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-6.0)

    In Program.cs, they included the file limits as an option targeting the page that performs the upload:

    // Add services to the container.
    builder.Services.AddRazorPages(options => 
    {
    options.Conventions
        .AddPageApplicationModelConvention("/[your cshtml page name with no extension]",
            model =>
            {
                model.Filters.Add(
                new RequestFormLimitsAttribute()
                {
                    // Set the limit to 256 MB
                    ValueLengthLimit = 268435456,
                    MultipartBodyLengthLimit = 268435456,
                    MultipartHeadersLengthLimit = 268435456
                });
               // model.Filters.Add(
               //     new RequestSizeLimitAttribute(268435456));
            });
    });
    

    In the documentation, they just added MultipartHeadersLengthLimit, but I included other limits you would be interested in changing too.

    In your page model include:

    [DisableRequestSizeLimit]
    [RequestFormLimits(MultipartBodyLengthLimit = 268435456)]
    public class YourPageNameModel : PageModel
    { ...}
    

    Note that to follow their example, you must update the FileHelper.cs with the proper file signature if you are trying to upload a different extension. Otherwise, the page will return an invalid state.

    Login or Signup to reply.
  3. This is a security + optimization feature. By default there is a limit on just how much content(quantity and quality) you can move per Payload. For .net core according to this article, it is about 25MB.

    Depending on how you want to host, you could change this limit to suit your case.

    You could add a web.config file to the project and add the code bellow.

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

    you could also follow the microsoft documentation(This is for MVC, but it serves well).

    Will that be enough? No. Due to the TCP/IP & IEEE Standard on Network structure, if you do not have an open pipeline, transfering large files can be a chore without good network. it can easily break.

    I would advice you to also try working on compressed files. This tutorial compresses file into a Zip.

     using(var compress = new GZipStream(outputFile, 
            CompressionMode.Compress, false)) 
     {
         byte[] b = new byte[inFile.Length];
         int read = inFile.Read(b, 0, b.Length);
         while (read > 0) 
         {
            compress.Write(b, 0, read);
            read = inFile.Read(b, 0, b.Length);
         }
     }.
    

    If you are dealing with Videos, however, i will advice against compressing to Zip or Rar. this is because videos are already very much compressed. you could however "traspose & transform", meaning that you could covert from an Avi to an Mkv. Sometimes, this process could me loss in data and quality.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search