.net 上传附件错误

发布时间 2023-10-27 11:46:18作者: qingjiawen

 

错误

net::ERR_CONNECTION_ABORTED 

  • 导致这种错误的主要原因是上传的文件太大,服务器不能继续读取请求而过早中断链接

Failed to load resource: the server responded with a status of 413 ()

开发环境(IISExpress)

1073741824=1GB
根目录下创建 web.config 文件,内容如下
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <security>
            <requestFiltering> 
                <requestLimits maxAllowedContentLength="1073741824" />
            </requestFiltering>
        </security>
    </system.webServer>
</configuration>

生产环境

通常生产环境 IIS 自动项目添加了 web.config 文件,这时候只需要在 <configuration> 节点下添加下面内容即可:

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="1073741824" />
        </requestFiltering>
    </security>
</system.webServer>

 对api方法中可以设置不限制大小,或者填入大小

 

nginx 上传限制 client_max_body_size

http {
    include       mime.types;
    default_type  application/octet-stream;
    client_max_body_size 1000m
}

 

前端项目vue axios

Uncaught (in promise) Error: timeout of 6000ms exceeded

 

Multipart body length limit 134217728 exceeded.

Post的body大概超过100多M会碰到这个错误

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    //解决Multipart body length limit 134217728 exceeded
    services.Configure(x =>
    {
        x.ValueLengthLimit = int.MaxValue;
        x.MultipartBodyLengthLimit = int.MaxValue; // In case of multipart
    });
}

 

 

IIS配置

打开iis,选择配置编辑器(修改两处,system.webServer/security/requestFiltering;system.web/httpRuntime)

 

 可以发现,最大上传值被限制到了30m,于是再后面加两个0,使其达到3G,再去试试