Note that there are some explanatory texts on larger screens.

plurals
  1. POError with a file upload WCF WEB API (Preview 6) : Cannot write more bytes to the buffer than the configured maximum buffer size: 65536
    text
    copied!<p>I'm having a real problem with the WCF web api.</p> <p>I have a simple method that uploads a file and saves to disk. I seem to have set all the right params, but get the above error message when I try to upload a 2mb file.</p> <p>Server Code:</p> <pre><code>public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); HttpServiceHostFactory _factory = new HttpServiceHostFactory(); var config = new HttpConfiguration() { EnableTestClient = true, IncludeExceptionDetail = true, TransferMode = System.ServiceModel.TransferMode.Streamed, MaxReceivedMessageSize = 4194304, MaxBufferSize = 4194304 }; _factory.Configuration = config; RouteTable.Routes.Add(new ServiceRoute("api/docmanage", _factory, typeof(WorksiteManagerApi))); } </code></pre> <p>client:</p> <pre><code>HttpClientHandler httpClientHandler = new HttpClientHandler(); httpClientHandler.MaxRequestContentBufferSize = 4194304; var byteArray = Encoding.ASCII.GetBytes(ConnectionSettings.WebUsername + ":" + ConnectionSettings.WebPassword); HttpClient httpClient = new HttpClient(httpClientHandler); httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); httpClient.BaseAddress = new Uri(ConnectionSettings.WebApiBaseUrl); httpClient.MaxResponseContentBufferSize = 4194304; ... multipartFormDataContent.Add(new FormUrlEncodedContent(formValues)); multipartFormDataContent.Add(byteArrayContent); var postTask = httpClient.PostAsync("api/docmanage/UploadFile", multipartFormDataContent); </code></pre> <p>Then, on the server:</p> <pre><code>[WebInvoke(Method = "POST")] public HttpResponseMessage UploadFile(HttpRequestMessage request) { // Verify that this is an HTML Form file upload request if (!request.Content.IsMimeMultipartContent("form-data")) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } // Create a stream provider for setting up output streams MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider(); // Read the MIME multipart content using the stream provider we just created. IEnumerable&lt;HttpContent&gt; bodyparts = request.Content.ReadAsMultipart(streamProvider); foreach (var part in bodyparts) { switch (part.Headers.ContentType.MediaType) { case "application/octet-stream": if (part.Headers.ContentLength.HasValue) { // BLOWS UP HERE: var byteArray = part.ReadAsByteArrayAsync().Result; if (null == fileName) { throw new HttpResponseException(HttpStatusCode.NotAcceptable); } else { uniqueFileId = Guid.NewGuid().ToString(); string tempFilename = Path.GetTempPath() + @"\" + uniqueFileId + fileName; using (FileStream fileStream = File.Create(tempFilename, byteArray.Length)) { fileStream.Write(byteArray, 0, byteArray.Length); } } } break; } } } </code></pre> <p>Any ideas? I am using the latest preview of the web api.... I noticed a lot is missing from the support documentation but it seems there is some buffer limit that I can't find out how to specify or is being ignored.....</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload