asp.net Core mvc file Upload Limit Problem example _ practical skills

Source: Internet
Author: User
Tags file upload httpcontext


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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.