Image cut after being uploaded to webapi

I use this code to upload images in asp.net webapi:

[HttpPost]
[Route("imageBrowser/insert")]
[SharePointContextWebAPIFilter]
public object PostFile()
{
    HttpRequestMessage request = this.Request;
    if (!request.Content.IsMimeMultipartContent())
    {
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }


    string root = System.Web.HttpContext.Current.Server.MapPath("~/Content/images");
    var provider = new MultipartFormDataStreamProvider(root);
    Request.Content.ReadAsMultipartAsync(provider);


    //return Request.CreateResponse(HttpStatusCode.OK); 
    return Request.CreateResponse(HttpStatusCode.OK);
}

The file is not being uploaded correctly, it looks like this

Is this related to azure? Is there something wrong with the code?

The problem is because you’re using async code in a non async method. So the thread is being finished before ending the read part.

try this:

[HttpPost]
[Route("imageBrowser/insert")]
[SharePointContextWebAPIFilter]
public async object PostFile()
{
    HttpRequestMessage request = this.Request;
    if (!request.Content.IsMimeMultipartContent())
    {
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }


    string root = System.Web.HttpContext.Current.Server.MapPath("~/Content/images");
    var provider = new MultipartFormDataStreamProvider(root);
    await Request.Content.ReadAsMultipartAsync(provider);


    //return Request.CreateResponse(HttpStatusCode.OK); 
    return Request.CreateResponse(HttpStatusCode.OK);
}