Resolve an ASP. NET Core mvc file upload limit problem instance

Source: Internet
Author: User
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.

  • 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.