I am having problems uploading images to the server via RestSharp.
I have a Wcf Rest service that accepts a Stream. If I use the code below, I always get this exception:
Protocol ViolationException The bytes that must be written to the stream exceed the specified byte size of the content length.
What settings do I need to configure ... setting the content length header does not seem to matter.
The server side does not receive the image, but a slightly smaller stream of bytes.
Any help was appreciated.
Client (test) code:
byte[] byteArray = File.ReadAllBytes("small.jpg"); request.AddHeader("Content-Length", int.MaxValue.ToString());//doesn't matter what length I put here request.AddFile("image/jpeg", (requestStream) => { using (var ms = new MemoryStream(byteArray)) { ms.CopyTo(requestStream, byteArray.Length);//doesn't matter whether I add second param or not ms.Flush(); ms.Close(); } }, "sample", "image/jpeg"); request.Method = Method.POST; client.ExecuteAsync(request, (response, a) => { Assert.IsNotNull(response.Content); string content = response.Content; resetEvent.Set(); });
Service Code (returns the URL of the saved image)
[OperationContract] [WebInvoke(UriTemplate = "upload/{fileName}/{fileExtension}/{id}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)] Message UploadPhoto(string fileName, String fileExtension, string id, Stream fileContents);
source share