how to get a httppostedfile from a ASP.NET Web API (POST or PUT) call?

asp.net-web-api, http-post, http-put

Solution

Your short question does not have a short answer I am afraid.

ASP.NET Web API exposes you to the wonders of HTTP while ASP.NET MVC abstracted some of it - in this case for HttpPostedFile.

So a bit of background:

HTTP posts where a file is involved usually has multipart form data content. This means that you are mixing different kind of content-type: your normal form data will be sent using formurlencoded while the files will be sent application/octent-stream.

So in Web API, all you have to do is to say

var contents = message.Content.ReadAsMultipartAsync(); // message is HttpRequestMessage

One of the contents will contain your file.

Problem

Actually my question is short. How can I get a HttpPostedFile from a ASP.NET Web API POST or PUT? I did see that I can get various information from the Request like Request.Header, Request.Content, Request.Properties. Where in there can I find the file I passed and how can I create a HttpPostedFile from it? Thanks in advance!

Original source