Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to upload file in Asp.Net Core with unlimited size but after lots of searches I didin't find any useful solutions.

If anyone have idea please share

What I have tried:

I have tried this link
Posted
Updated 9-Jan-19 4:10am

I actually stumbled on your question as I was searching for a solution myself this morning. Someone else sent me a link and it gave me enough to get me going: iis - Increasing max. request length in asp.net 5 (vNext) - Stack Overflow[^]

Mine was RC1 (had trouble porting to RC2), so you may need to tweak a little, but my web.config ended up being:

XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <httpRuntime executionTimeout="100000" maxRequestLength="214748364" />
  </system.web>
  <system.webServer>
    <handlers>
      <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
    </handlers>
    <!--<httpPlatform processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" startupTimeLimit="3600" stdoutLogEnabled="false" />-->
    <httpPlatform processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600"/>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="4294967295" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>
 
Share this answer
 
v2
In the case of a .NET CORE application, the configuration can be achieved in a much more easier manner. The easiest way to do this, would be to add the RequestSizeLimit Attribute to the Model. For example:
C#
[RequestSizeLimit(5*1024*1024)]     // set the maximum file size limit to 5 MB
public class InputModel 
{
    ...
}


It would also help adopting a Validation Attribute to verify the uploaded file size is limited to the specific size you are seeking. For example:
C#
namespace MyApp.Validators
{
    using Microsoft.AspNetCore.Http;
    using System.ComponentModel.DataAnnotations;

    [AttributeUsage(
        validOn: AttributeTargets.Field | AttributeTargets.Property, 
        AllowMultiple = false, 
        Inherited = true)]
    public class MyUploadFileSizeValidator
        : ValidationAttribute
    {
        public MyUploadFileSizeValidator(long sizeInBytes)
        {
            this.SizeInBytes = sizeInBytes;
        }

        public long SizeInBytes { get; private set; }


        /// <summary>  
        ///     Validates the specified value with respect to the current validation attribute
        /// </summary>  
        /// <param name="value">the value to validate</param>  
        /// <returns>Returns - true to specify size is okay.</returns> 
        public override bool IsValid(object value)
        {
            bool isValid = false;

            // NOTE: Use HttpPostedFileBase instead of IFormFile in ASP.NET MVC
            if (value is IFormFile file)
            {
                isValid = file.Length <= this.SizeInBytes;
            }

            return isValid;
        }

    }
}


and then, just use it on the property of the Model. So, your model would look something like this:

C#
[RequestSizeLimit(5*1024*1024)]     // set the maximum file size limit to 5 MB
public class InputModel
{
    [Required(ErrorMessage = "Please select an image file to upload.")]
    [MyUploadFileSizeValidator(sizeInBytes: 5 * 1024 * 1024, 
                               ErrorMessage = "Image filesize should be smaller than 5 MB")]
    [Display(Name = "Profile Photo")]
    public IFormFile ProfilePhoto { get; set; }
}


Hope this helps.
 
Share this answer
 
Comments
Richard MacCutchan 9-Jan-19 10:11am    
Why have you posted this twice? Please delete the one that is not correct.
jaket-cp 9-Jan-19 10:43am    
Also its an answer for a question made in 2016 :)
Member 12003880 9-Jan-19 11:23am    
So what? My response was with an intention to help anyone who might search for the same - with respect to .NET CORE 2.1 or above.
jaket-cp 11-Jan-19 4:15am    
ok - agree with you entirely - new rules, new game :)
happy coding
CHill60 9-Jan-19 11:43am    
As long as the poster is bringing something new to the discussion then resurrecting old posts with new solutions is acceptable (there is a message from Chris somewhere about it).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900