I have to create a Web API (I'm using ASP.NET Core 1.1), and one of the things it needs to be able to do is to receive a JPEG image (and later also other document types such as PDF).
The front end developer (using Angular2) said he will send the image to the API in Base64. I had previously created a controller action in my API that accepts an IFormFile
, but the front end developer said he doesn't know how to send the API in this way.
So I now need to modify my API to accept a Base64 image. Can I have a simple controller action in the API which reads the body of the POST request, and then converts that into a file?
So:
// POST: api/images
[HttpPost]
public async Task<IActionResult> PostImage([FromBody] string image)
{
byte[] bytes = Convert.FromBase64String(image);
using (MemoryStream ms = new MemoryStream(bytes))
Image image = Image.FromStream(ms);
return CreatedAtAction("PostImage", new { id = 123});
}
Should that do the trick?