从POST请求中提取jpeg

时间:2017-03-22 09:31:10

标签: c# asp.net-web-api2 jpeg

如何从POST请求中将jpeg提取到文件中? POST请求标头不是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");
}

1 个答案:

答案 0 :(得分:1)

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

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