First, Introduction
In asp.net Core mvc, file upload the largest upload file defaults to 20MB, if we want to upload some of the larger files, 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 to set the file upload size limit to MB.
public void Configureservices (iservicecollection services)
{
servicesconfigure<formoptions> (options =>
{
optionsmultipartbodylengthlimit = 60000000;}
);
2.Action level setting
In addition to the global settings above, we can also control a single action by customizing the filter code 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 has not been read yet, so set the limits
featuresSet<IFormFeature>(new FormFeature(contextHttpContextRequest, _formOptions));
}
}
}
Because in asp.net Core mvc, unlike previous versions, specific functions are encapsulated in various feature (features), and HttpContext contexts are only containers that can be managed with each feature. This filter will only intercept the action, the HttpContext in the Formfeature (responsible for the form submission function) reset, so as to limit the specific action upload file size.
Third, the conclusion
It felt like a bug was found in a file upload and has been confirmed to have been repaired in version 1.0.1. In version 1.0.0, if the action does not set a ifromfile as an argument, then Request.From.Files will not be able to access and report an exception.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.