前言:在web项目的.net framework时文件上传时,自己常用一般处理程序接受上传文件,上传文件的大小限制是可以项目的webconfig里配置。   到core项目使用一般处理程序变成了中间件,但是使用中间件接受的时候,就遇到了上传大文件时,抛出的异常:

httprequest.form threw an exception of type microsoft.aspnetcore.server.kestrel.core.badhttprequestexception : request body too large  ,说是请求内容太大了,接收不了,就查了查各种文章:

  先是在官网里看上传文件的文档:   里面有对上传文件请求大小限制的描述,但是其解决方案无法使用,很奇怪!

 

 

 将上面的代码复制到项目中:

 

 

 先是报错,无法使用!   可能这种解决方案不适用,或者在.net core 3.0的web_razor项目中无法使用!

 

  后来干脆放弃这种方案,还是从度娘里搜吧,看看其他道友有没有类似的错误,当然是换一个搜法:直接搜跟异常内容相关的,果然看到了类似的文章:

 

 

 但是人家是加了usekestrel方法,不过是放在了usestartup方法的后面,跟官网的解决方案位置不一样,代码倒是一样的,这这这!!!   是官方错了吗???   擦擦擦,不管了,试试吧,结果发现不报错了,上传大文件也ok了,我草!  群众的力量才是伟大的!

 

下面贴出使用.net core 3.0 web_razor项目,借助中间件处理大文件上传的代码,供其他道友参考:

  1-program.cs:

public class program
    {
        public static void main(string[] args)
        {
            createhostbuilder(args).build().run();
        }

        public static ihostbuilder createhostbuilder(string[] args) =>
            host.createdefaultbuilder(args).configurewebhostdefaults(webbuilder =>
                {
                    webbuilder.usestartup<startup>().usekestrel(options =>
                    {
                        //控制中间件允许的上传文件大小为:不限制
                        options.limits.maxrequestbodysize = null;
                    });
                }).configurelogging(logging =>
                {
                    //https://github.com/nlog/nlog/wiki/getting-started-with-asp.net-core-3
                    logging.clearproviders();
                    logging.setminimumlevel(loglevel.debug);
                }).usenlog();
    }

2-startup.cs配置服务模块-注册中间件: 

//如果请求地址符合登录中间件,则将其发送到登录中间件处理
            //管道中间件
            app.mapwhen(context => context.request.path.tostring().endswith("/ajax/report.js"), appbranch => appbranch.usemiddleware<reportmiddleware>());

3-razor页面上传代码:

  

@*表单控件*@
            <form method="post" enctype="multipart/form-data" action="#">
                <div class="form-group">
                    选择文件:<input type="file" name="file" id="uploadfiles" multiple />
                </div>
                <button type="submit" class="btn btn-primary" id="btnsubmit">提交</button>
            </form>
<script type="text/javascript">
        $("#btnsubmit").click(function () {
            var _$that = $(this);
            _$that.attr('disabled', true);

            var formdata = new formdata();
            var files = $("#uploadfiles")[0].files;
            for (var i = 0; i < files.length; i++) {
                formdata.append("file" + i, files[i]);
            }

            var result = null;
            //模拟form表单上传
            $.ajax({
                type: "post",
                url: "/ajax/report.js?action=oneuploadfiles",
                data: formdata,
                //false代表只有在等待ajax执行完毕后才执行
                async: true,
                processdata: false,
                contenttype: false,
                success: function (json) {
                    try {
                        result = json.parse(json);
                    } catch (e) {
                        result = new function("return " + json)();
                    }
                    alert(result);
                },
                error: function (e) {
                    console.log(e);
                }
            });

        });

    </script>

4-中间件接受文件:

httprequest httprequest = httpcontext.request;
httpresponse httpresponse = httpcontext.response;
iformfilecollection filecoll = httprequest.form.files;