2017-03-22 106 views
1

如何從POST請求中將jpeg提取到文件中? POST請求頭不MimeMultipartContent :(所有在網絡上的例子似乎表明this.Request.Content.Headers.Contenttype.MediaType包含字符串image/jpegDebug.Trace(this.Request.Content)只是給了我"System.Web.Http.WebHost.HttpControllerHandler+LazyStreamContent"從POST請求中提取jpeg

這裏是我到目前爲止的代碼:

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

提取內容流'var imageStream = await request。 Content.ReadAsStreamAsync()'從那裏你可以做任何你想要的流。 – Nkosi

+0

僅供參考:我試圖在https://www.axis.com/gb/en/support/technical-notes/upload-images-video-to-http-server – Caltor

回答

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

上編寫一個C#等效的perl cgi腳本高超!謝謝 對於其他人蔘考保存到磁盤,您可以使用: using(var FileStream = File.Create(destination)) { ImageStream.Seek(0,SeekOrigin.Begin); \t ImageStream.CopyTo(FileStream); } – Caltor