tprocopio says:
private
string
ProcessaUploadedFile(PessoaViewModel model)
You need to define your method public inorder to call by the Webcam.upload method.
Inside the method retrieve the captured image from Request.Form.Files collection. Then save the file in folder using FileStream class and then write the code for saving in database.
Refer below sample and correct your code accordingly.
Controller
public class HomeController : Controller
{
private IWebHostEnvironment Environment;
public HomeController(IWebHostEnvironment _environment)
{
Environment = _environment;
}
public IActionResult Index()
{
return View();
}
public string ProcessaUploadedFile()
{
// Fetch the captured file in IFormFile.
IFormFile file = Request.Form.Files[0];
string fileName = DateTime.Now.ToString("dd-MM-yy hh-mm-ss");
// Setting file Path.
string filePath = string.Format(Path.Combine(this.Environment.WebRootPath, "Uploads/{0}.jpg"), fileName);
using (FileStream stream = new FileStream(filePath, FileMode.Create))
{
file.CopyTo(stream);
}
// Code for inserting data in database.
return fileName;
}
}
View
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th align="center"><u>Live Camera</u></th>
<th align="center"><u>Captured Picture</u></th>
</tr>
<tr>
<td><div id="webcam"></div></td>
<td><img id="imgCapture" /></td>
</tr>
<tr>
<td align="center">
<input type="button" id="btnCapture" value="Capture" />
</td>
<td align="center">
</td>
</tr>
</table>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/webcamjs/1.0.26/webcam.js"></script>
<script type="text/javascript">
$(function () {
Webcam.set({
width: 320,
height: 240,
image_format: 'jpeg',
jpeg_quality: 90
});
Webcam.attach('#webcam');
$("#btnCapture").click(function () {
Webcam.snap(function (data_uri) {
$("#imgCapture")[0].src = data_uri;
Webcam.upload(data_uri, 'Home/ProcessaUploadedFile', function (code, text) {
alert('Photo Captured')
});
});
});
});
</script>
</body>
</html>
For insert data into database please refer below article.