I'm trying to post an uploaded image from the front-end using aurelia to a asp.net mvc. Is there someway I can save the image received in as png format in a folder on the server?
Javascript method from where I'm posting the image data(files is the uploaded image and id is the unique image ID that I need to use for the file name)
saveImage(files,id) {
var form = new FormData()
form.append('file', files)
form.append('ID', id)
this.http.fetch('/api/Employees', {
method: 'post',
body: form
})
return true;
}
ImagesController(Asp.net MVC)
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace SPAproject.Controllers
{
public class ImagesController : Controller
{
[HttpPost, Route("api/[controller]")]
public async Task<IActionResult> SaveImage()
{
try
{
var form = await Request.ReadFormAsync();
var file = form.Files.First();
var id = form.ElementAt(1);
var path = "/images/" + id + ".png";
Stream stream = file.OpenReadStream();
return Ok(true);
}
catch (Exception ex)
{
var originalMessage = ex.Message;
while (ex.InnerException != null)
ex = ex.InnerException;
return BadRequest($"{originalMessage} | {ex.Message}");
}
}
}
}
}