First, Introduction
in ASP. NET Core MVC, the file uploads the largest upload file by default to 20MB, if we want to upload some of the larger files, we do not know how to set up, without the Web. config How should we do?
Second, set the upload file size
1. Application-level settings
We need to add the following code to the Configureservices method, and set the file upload size limit to a few megabytes.
public void Configureservices (iservicecollection services) { servicesconfigure<formoptions> (options = { optionsmultipartbodylengthlimit = 60000000; });}
2.Action level settings
In addition to setting the global settings above, we can also control a single action by customizing the filter, the filter code is as follows:
[AttributeUsage (Attributetargetsclass | Attributetargetsmethod, AllowMultiple = False, inherited = True)] public class Requestformsizelimitattribute:attribute, Iauthorizationfilter, Iorderedfilter { private readonly formoptions _formoptions; Public requestformsizelimitattribute (int valuecountlimit) { _formoptions = new Formoptions () { Valuecountlimit = Valuecountlimit }; } public int Order {get; set;} public void Onauthorization (Authorizationfiltercontext context) { var features = Contexthttpcontextfeatures; var formfeature = featuresget<iformfeature> (); if (formfeature = = NULL | | formfeatureform = = NULL) { //Request form have not been read yet, so set the Limits
featuresset<iformfeature> (New Formfeature (Contexthttpcontextrequest, _formoptions)); } } }
Because the feature is encapsulated in various feature (features), the HttpContext context is simply a container that can be managed by each feature, unlike the previous version in ASP. This filter will only intercept the action, HttpContext in the Formfeature (responsible for the form submission function) re-set, so as to limit the specific action upload file size.
Third, conclusion
It felt like a bug that found a file upload, which has been fixed in version 1.0.1. In 1.0.0, if the action does not set a ifromfile as a parameter, then Request.From.Files will not be able to access and report an exception.
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we have a lot of topic.alibabacloud.com.