尝试使用HttpClient上传文件时,Web Api的RequestEntityTooLarge响应

我正在尝试使用Asp.Net Web Api创建一个通过Post上传文件的Web服务。 这些分别是Client和Web Api的实现:

客户:

using (var client = new HttpClient()) { client.BaseAddress = new Uri("https://127.0.0.1:44444/"); using (var content = new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture))) { content.Add(new ByteArrayContent(File.ReadAllBytes(filepath))); content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); var response = await client.PostAsync("api/Synchronization", content); if (response.IsSuccessStatusCode) eventLog1.WriteEntry("Synchronization has been successful", EventLogEntryType.Information); else eventLog1.WriteEntry(response.StatusCode + ":" + response.ReasonPhrase, EventLogEntryType.Error); } } 

服务器:

 public class SynchronizationController : ApiController { public HttpResponseMessage SynchronizeCsv() { var task = this.Request.Content.ReadAsStreamAsync(); task.Wait(); Stream requestStream = task.Result; try { Stream fileStream = File.Create(HttpContext.Current.Server.MapPath(path)); requestStream.CopyTo(fileStream); fileStream.Close(); requestStream.Close(); } catch (IOException) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "A generic error occured. Please try again later."); } HttpResponseMessage response = new HttpResponseMessage(); response.StatusCode = HttpStatusCode.Created; return response; } } 

Web.config文件:

                  

我正在尝试上传一个5Mb的文件,每次我尝试调用该服务时,都会得到413 Http错误请求实体太大。 如果文件很小(例如40kb),一切正常。

通过互联网查看我试图对web.config进行一些修改,但似乎没有任何正常工作。 我究竟做错了什么?

我通过这个解决方案解决了这个问题: http : //www.smartdigitizers.com/net-development-tips/iis-7-how-to-resolve-error-https-413-request-entity-too-large/

  1. 启动“Internet信息服务(IIS)管理器”
  2. 选择您在其下托管Web应用程序的站点。
  3. 在“function”部分中,双击“配置编辑器”
  4. 在“Section”下,选择:system.webServer,然后选择serverRuntime
  5. 将“uploadReadAheadSize”部分修改为20MB(以字节为单位的值)
  6. 单击“应用”