从 POST 请求中提取 jpeg

Extracting jpeg from POST request

如何从 POST 请求中提取 jpeg 到文件中? POST 请求 header 不是 MimeMultipartContent :( 网络上的所有示例似乎都显示了这一点。this.Request.Content.Headers.Contenttype.MediaType 包含字符串 image/jpegDebug.Trace(this.Request.Content) 就给我 "System.Web.Http.WebHost.HttpControllerHandler+LazyStreamContent"

到目前为止,这是我的代码:

public IHttpActionResult Post()
{
    HttpRequestMessage request = this.Request;
    Trace.WriteLine(request.Content.Headers.ContentType.MediaType);  // "image/jpeg"

    // Do the extraction to a file here

    return Ok("Worked");
}

从请求中提取内容流。从那里你可以用流做你想做的事。

public async Task<IHttpActionResult> Post() {
    var request = this.Request;
    Trace.WriteLine(request.Content.Headers.ContentType.MediaType);  // "image/jpeg"

    var imageStream = await request.Content.ReadAsStreamAsync();

    //...save stream to disk or database...etc.    

    return Ok("Worked");
}