web api 2.0 上传文件超过4M时,出现404错误

客户端代码

string path = "C:\text.txt";
WebClient client = new WebClient();
Uri _address = new Uri(_baseAddress, "/api/Basedata/UploadDat/");
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//长度
client.UploadFile(_address, "POST", path);

服务器端代码

[HttpPost]
        public void UploadDat()
        {
            HttpPostedFile file = HttpContext.Current.Request.Files[0];
            string strPath = ConfigurationManager.AppSettings["UploadServerDatPath"];
            if (!Directory.Exists(strPath))
            {
                Directory.CreateDirectory(strPath);
            }
            string Path = strPath + file.FileName;
            file.SaveAs(Path);
        }

部署后,上传大于4M的文件出现404错误,说明根本就没有找到这个服务器地址。

首先,web api有设置默认上传文件大小最大是4M,在服务器端的web.config中需要添加如下配置:

<httpRuntime targetFramework=“4.5” maxRequestLength="2097152" executionTimeout="3600"/>

另外,IIS也要做修改,请求筛选-->编辑功能设置-->允许的最大内容长度改为2147483648,并重新启动服务。

原文地址:https://www.cnblogs.com/ttssrs/p/6737324.html