上传到webapi后剪切图像

时间:2017-07-25 21:23:31

标签: image azure asp.net-web-api2

我使用此代码在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);
}

文件未正确上传,看起来像这样 enter image description here

这与天蓝色有关吗?代码有问题吗?

1 个答案:

答案 0 :(得分:0)

问题是因为您在非异步方法中使用异步代码。所以线程在结束读取部分之前完成。

试试这个:

[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);
}