Mvc icon indicating copy to clipboard operation
Mvc copied to clipboard

Overriding Request Form max upload limit

Open muratg opened this issue 10 years ago • 34 comments

From @Tratcher on May 27, 2016 18:33

From @Ryuuke on May 27, 2016 18:30

Hello, I encountered this error when uploading large file (~200MB).

An unhandled exception occurred while processing the request. Multipart body length limit 134217728 exceeded at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream.UpdatePosition(Int32 read)

I already tried to modify my web.config file : httpRuntime maxRequestLength="1073741824" executionTimeout="300000" and requestLimits maxAllowedContentLength="1073741824"

but still, I get the same error.

I'm using the RC2 version, is there a way to override that limit ?

Copied from original issue: aspnet/KestrelHttpServer#891

Copied from original issue: aspnet/HttpAbstractions#641

muratg avatar Aug 10 '16 21:08 muratg

From @Tratcher on May 27, 2016 18:38

There are limits on the request form. Where are you trying to read the Form, an MVC Controller/Action?

The limits can be adjusted per action like this: https://github.com/aspnet/Entropy/blob/dev/samples/Mvc.FileUpload/Controllers/RequestFormLimitsController.cs#L19

Or for the whole app like this (but don't do this just for one file): https://github.com/aspnet/Entropy/blob/dev/samples/Mvc.FileUpload/Startup.cs#L34

muratg avatar Aug 10 '16 21:08 muratg

From @davidfowl on May 27, 2016 18:40

@Tratcher @Eilon @kichalla should that filter be built into mvc?

muratg avatar Aug 10 '16 21:08 muratg

From @Tratcher on May 27, 2016 18:42

@rynowak Probably.

muratg avatar Aug 10 '16 21:08 muratg

From @Tratcher on May 27, 2016 18:43

https://github.com/aspnet/Entropy/blob/dev/samples/Mvc.FileUpload/Filters/RequestFormSizeLimitAttribute.cs

muratg avatar Aug 10 '16 21:08 muratg

From @Ryuuke on May 27, 2016 19:7

Hey, thanks for your reply, yeah i'm trying to read the form from the an MVC action, it was the MultipartBodyLengthLimit property inside the FormOptions that I updated to a larger value than the default and It Worked well.

muratg avatar Aug 10 '16 21:08 muratg

From @victorhurdugaci on July 6, 2016 22:16

This is something that should be in MVC. cc @kichalla @rynowak

muratg avatar Aug 10 '16 21:08 muratg

From @Eilon on July 7, 2016 1:52

Is this the same as https://github.com/aspnet/Mvc/issues/481?

muratg avatar Aug 10 '16 21:08 muratg

From @Tratcher on July 7, 2016 2:21

Not quite, this is specific to forms.

muratg avatar Aug 10 '16 21:08 muratg

We should revisit what exactly we want to do here.

danroth27 avatar Aug 19 '16 05:08 danroth27

What am I missing? We added upload limits to kestrel by default, and also added an override to MVC in a sample

rynowak avatar Aug 19 '16 15:08 rynowak

I think the proposal was to add a MVC filter to set the available form options here https://github.com/aspnet/HttpAbstractions/blob/8b3c308c226dd61532e543697d9d35bb94aba823/src/Microsoft.AspNetCore.Http/Features/FormOptions.cs. There is a sample in Entropy that illustrate this mentioned above but I think the discussion was whether we should polish that and ship it.

JunTaoLuo avatar Aug 19 '16 16:08 JunTaoLuo

Upgraded my project to latest versions 1.1.1 along with the tooling upgrade in VS2017. The work around mentioned doesn't seem to work anymore. When I'm debugging I can see the options code gets run and the values are updated. The controller just doesn't seem to recognize the change.

Keep getting "Multipart body length limit 16384 exceeded." even though I have this in my startup.

			services.Configure<FormOptions>(options =>
			{
				options.ValueLengthLimit = int.MaxValue;
				options.MultipartBodyLengthLimit = long.MaxValue;
			});

			services.AddMvc()
				.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
				.AddDataAnnotationsLocalization();

JasonRowe avatar Mar 09 '17 20:03 JasonRowe

@JasonRowe, I ran into that issue earlier today, I upgraded a project to 1.1.1 yesterday.

For me I believe having the RequestFromSizeAttribute on the method fixed the problem. I haven't had time to dig in and actually figure out what fixed it.

Heres an example:

[HttpPost("api/[controller]/upload")] [RequestFormSizeLimit(500, 1024 * 1024 * 25)] //500 - ValueCount Limit, 25 MB MaxBodyPartLengthLimit public async Task Upload(FileDescription fileDescription){...

jacksondaw avatar Mar 09 '17 20:03 jacksondaw

Also tried adding a form attribute with no luck. Nothing I do seems to effect the MultipartBodyLengthLimit. Anyone have a work around?

Here was what my attribute was doing:

public RequestFormSizeLimitAttribute(int valueCountLimit, long multipartBodyLengthLimit)
		{
			_formOptions = new FormOptions()
			{
				ValueCountLimit = valueCountLimit,
				MultipartBodyLengthLimit = multipartBodyLengthLimit
			};
		}

JasonRowe avatar Mar 09 '17 23:03 JasonRowe

/cc @Tratcher @halter73 - what's the right setting for this?

rynowak avatar Mar 09 '17 23:03 rynowak

I also tried the "DisableFormValueModelBindingAttribute" attribute from these old docs and it didn't help.

https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads

https://raw.githubusercontent.com/aspnet/Docs/master/aspnetcore/mvc/models/file-uploads/sample/FileUploadSample/Filters/DisableFormValueModelBindingAttribute.cs

JasonRowe avatar Mar 10 '17 00:03 JasonRowe

@rynowak Here are the Kestrel limits that can be configured today.

While you can set absolute limits for the request line and request headers sizes, there is none for the request body. The MaxRequestBufferSize only limits the number of request body bytes that can sit in Kestrel's internal buffers without the app reading them before back pressure is applied.

halter73 avatar Mar 13 '17 19:03 halter73

Not very helpful but suddenly I can't recreate my issue. I did change some settings that made my virtual machine run faster (added additional CPU's). Now I don't appear to need any settings in the startup and the same file works fine via the same form without changes.

JasonRowe avatar Mar 14 '17 00:03 JasonRowe

/cc @Tratcher did you have any thoughts on this?

rynowak avatar Mar 15 '17 20:03 rynowak

Not if he can't reproduce it. It would be nice to make the sample filter/attribute a shipping thing.

Tratcher avatar Mar 17 '17 22:03 Tratcher