解决ASP.NET Core Mvc文件上传限制问题

涓€銆佺畝浠?/span>

銆€銆€鍦ˋSP.NET Core MVC涓?鏂囦欢涓婁紶鐨勬渶澶т笂浼犳枃浠堕粯璁や负20MB,濡傛灉鎴戜滑鎯充笂浼犱竴浜涙瘮杈冨ぇ鐨勬枃浠讹紝灏变笉鐭ラ亾鎬庝箞鍘昏缃簡锛屾病鏈変簡Web.Config鎴戜滑搴旇濡備綍涓嬫墜鍛紵

浜屻€佽缃笂浼犳枃浠跺ぇ灏?/span>

銆€銆€1.搴旂敤绋嬪簭绾у埆璁剧疆

銆€銆€銆€銆€鎴戜滑闇€瑕佸湪 ConfigureServices鏂规硶涓坊鍔犲涓嬩唬鐮侊紝璁剧疆鏂囦欢涓婁紶鐨勫ぇ灏忛檺鍒朵负60 MB銆?/span>

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<FormOptions>(options =>
    {
        options.MultipartBodyLengthLimit = 60000000;
    });
}

銆€銆€2.Action绾у埆璁剧疆

銆€銆€銆€銆€闄や簡涓婇潰瀵瑰叏灞€杩涜璁剧疆澶栵紝鎴戜滑涔熷彲浠ラ€氳繃鑷畾涔塅ilter鐨勬柟寮忥紝瀵瑰崟涓狝ction杩涜鎺у埗锛孎ilter浠g爜濡備笅锛?/span>

  [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, 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 = context.HttpContext.Features;
            var formFeature = features.Get<IFormFeature>();

            if (formFeature == null || formFeature.Form == null)
            {
                // Request form has not been read yet, so set the limits
                features.Set<IFormFeature>(new FormFeature(context.HttpContext.Request, _formOptions));
            }
        }
    }

銆€銆€銆€鍥犱负ASP.NET Core MVC涓紝涓庝箣鍓嶇増鏈笉鍚岀殑鏄叿浣撳姛鑳借灏佽鍦ㄤ簡鍚勭Feature锛堢壒寰侊級涓紝HttpContext涓婁笅鏂囧彧鏄兘鍚勪釜鐗瑰緛杩涜绠$悊鐨勫鍣ㄣ€傝繖涓狥ilter涓氨鍙皢Action鎷︽埅锛屽HttpContext涓殑FormFeature锛堣礋璐h〃鍗曟彁浜ゅ姛鑳斤級閲嶆柊杩涜璁剧疆锛屼粠鑰岃揪鍒伴檺鍒跺叿浣揂ction涓婁紶鏂囦欢澶у皬鐨勭洰鐨勩€?/span>

涓夈€佺粨璇?/span>

銆€銆€銆€鏈潵鎰熻鍍忔槸鍙戠幇浜嗕竴涓枃浠朵笂浼犵殑BUG锛屽凡缁忕‘璁ゅ湪1.0.1鐗堟湰涓凡缁忚繘琛屼簡淇銆傚湪1.0.0鐗堟湰涓紝濡傛灉Action娌℃湁璁剧疆涓€涓狪FromFile浣滀负鍙傛暟锛岄偅涔圧equest.From.Files灏嗕笉鑳借闂苟涓旀姤鍑哄紓甯搞€?/span>

 

GitHub锛歨ttps://github.com/maxzhang1985/YOYOFx  濡傛灉瑙夎繕鍙互璇?strong>Star涓嬶紝 娆㈣繋涓€璧蜂氦娴併€?/span>

 

.NET Core 寮€婧愬涔犵兢锛?nbsp;214741894  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值