I'm using the Media Capture and Streams API to connect to webcams and other peripheral cameras. We use the cameras to take still photos. My question is how can I POST or upload those photos to the server?
I'm capturing photos like so:
captureImage: function () {
// canvas is just a <canvas> element
const context = canvas.getContext("2d");
canvas.width = 320;
canvas.height = 240;
context.drawImage(video, 0, 0, 320, 240); // video is a <video> tag with live feed of camera
return canvas.toDataURL("image/png"); // can be used to set src of <img>
}
How can I POST the image to my .net endpoint so I can save it in Azure? So far I have tried:
var imageData = document.getElementById("img-canvas").toDataURL("image/png");
imageData = imageData.replace('data:image/png;base64,', '');
var TestViewModel = {
UploadFiles: document.getElementById("img-canvas").toDataURL("image/png"),
ImageData: imageData
}
$.ajax({
type: "POST",
url: "/Orders/Evaluation/UploadFiles",
data: TestViewModel, //JSON.stringify(TestViewModel),
//contentType: 'application/json',
dataType: "json",
async: true,
success: function (response) {
console.log("success");
},
error: function (response) {
console.log("fail");
},
});
On the server side, I have a simple endpoint setup:
[HttpPost("Orders/UploadFiles")]
public async Task<IActionResult> UploadPhotos(TestViewModel viewModel)
{
// save photo to Azure
}
And the TestViewModel looks like:
public class TestViewModel
{
public IFormFile UploadFiles { get; set; }
public string ImageData { get; set; }
}
The problem I'm having is the viewModel.UploadFiles
is always null. The viewModel.ImageData
is a string that I need to convert into a file. I don't know if either of these options is even appropriate or if there is a better way to upload captured images that aren't yet files.