Dear Experts,
Good day, asking for your help.
I have a project that will capture an image of a document from a webcam, capturing image could be 1 or more image, after capturing the image it will save it to the designated 'folder' as the same time on the database also ..
my problem is that after clicking the button, it won't saved the capture image.
here's some of the source code...
Model's:
using System.ComponentModel.DataAnnotations;
namespace FilMS.Models
{
public class DocInfo
{
public DocInfo() { }
public int Id { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}" ,ApplyFormatInEditMode = true)]
public DateTime? Received { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}", ApplyFormatInEditMode = true)]
public DateTime? Date { get; set; }
public virtual List<DocPhoto> DocPhotos { get; set; } = new List<DocPhoto>();
public string? Particulars { get; set; }
public string? FileNo { get; set; }
public string? RackNo { get; set; }
public string? CreatedBy { get; set; } = string.Empty;
public string? UpdatedBy { get; set; } = string.Empty;
public DateTime? Modified { get; set; }
}
}
using System.ComponentModel.DataAnnotations;
namespace FilMS.Models
{
public class DocPhoto
{
public DocPhoto() { }
[Key]
public int Photo_Id { get; set; }
[ForeignKey("DocInfo")]
public int DocInfo_Id { get; set; }
public virtual DocInfo DocInfo { get; set; }
public string? Doc_Image { get; set; }
}
}
Controller:
using FilMS.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
namespace FilMS.Controllers
{
public class DocInfoController : Controller
{
private readonly FilMSDbContext _context;
private readonly IWebHostEnvironment _webHost;
public DocInfoController(FilMSDbContext context, IWebHostEnvironment environment)
{
this._context = context;
this._webHost = environment;
}
public IActionResult Index()
{
List<DocInfo> docInfo = _context.DocInfos.OrderBy(d => d.Received).ToList();
return View(docInfo);
}
[HttpGet]
public IActionResult Add()
{
DocInfo docInfo = new DocInfo();
return View(docInfo);
}
[HttpPost]
public async Task<IActionResult> Add(DocInfo docInfo)
{
await _context.AddAsync(docInfo);
await _context.SaveChangesAsync();
int Id_No = docInfo.Id;
return RedirectToAction("Edit", new { Id_No });
}
[HttpGet]
public IActionResult Edit(int Id)
{
DocInfo docInfo = _context.DocInfos
.Include(dd => dd.DocPhotos).Where(di => di.Id == Id).FirstOrDefault();
return View(docInfo);
}
[HttpPost]
public IActionResult Edit(DocPhoto docPhoto,string name)
{
try
{
var files = HttpContext.Request.Form.Files;
if (files != null)
{
foreach (var file in files)
{
if (file.Length > 0)
{
var fileName = file.FileName;
var myUniqueFileName = Convert.ToString(Guid.NewGuid());
var fileExtension = Path.GetExtension(fileName);
var newFileName = string.Concat(myUniqueFileName, fileExtension);
var filePath = Path.Combine(_webHost.WebRootPath, "CameraPhotos") + $@"\{newFileName}";
if (!string.IsNullOrEmpty(filePath))
{
StoreInFolder(file, filePath);
}
var imageBytes = System.IO.File.ReadAllBytes(filePath);
if (imageBytes != null)
{
StoreInDatabase(imageBytes);
}
}
}
return Json(true);
}
else
{
return Json(false);
}
}
catch (Exception)
{
throw;
}
}
public void StoreInFolder(IFormFile file, string fileName)
{
using (FileStream fs = System.IO.File.Create(fileName))
{
file.CopyTo(fs);
fs.Flush();
}
}
private void StoreInDatabase(byte[] imageBytes)
{
// Saving captured into database
try
{
if (imageBytes != null)
{
string base64String = Convert.ToBase64String(imageBytes, 0, imageBytes.Length);
string imageUrl = string.Concat("data:image/jpg;base64", base64String);
DocPhoto docPhoto = new DocPhoto()
{
Photo_Id = 0,
Doc_Image = imageUrl
};
_context.DocPhotos.Add(docPhoto);
_context.SaveChanges();
}
}
catch (Exception)
{
throw;
}
}
}
}
Views:
Add.cshtml
@model FilMS.Models.DocInfo
@{
ViewData["Title"] = "New Document";
}
<h1>New Document</h1>
@* <i class="fa-solid fa-file-lines"></i> *@
@* <h4>DocInfo</h4> *@
<hr />
<div class="row">
@* <div class="col-md-12 mx-auto pt-0 p-0"> *@
<div class="col-md-12">
<form asp-action="Add" id="DocInfoEntryForm" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="col-md-12">
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label asp-for="Received" class="control-label">Date Received</label>
<input asp-for="Received" class="form-control" asp-format="{0:MM/dd/yyyy}" />
<span asp-validation-for="Received" class="text-danger"></span>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label asp-for="Date" class="control-label">Date Created</label>
<input asp-for="Date" class="form-control" asp-format="{0:MM/dd/yyyy}" />
<span asp-validation-for="Date" class="text-danger"></span>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label asp-for="Particulars" class="control-label"></label>
<textarea asp-for="Particulars" class="form-control"></textarea>
<span asp-validation-for="Particulars" class="text-danger"></span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label asp-for="FileNo" class="control-label">File No</label>
<input asp-for="FileNo" class="form-control" />
<span asp-validation-for="FileNo" class="text-danger"></span>
</div>
</div>
</div>
</div>
<hr />
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Edit.cshtml
@model IEnumerable<FilMS.Models.DocInfo>
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-action="Edit">Edit Record</a>
</p>
<table class="table">
<thead>
<tr>
<th>
Received
</th>
<th>
Date
</th>
<th>
Particulars
</th>
<th>
File No
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Received)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.Particulars)
</td>
<td>
@Html.DisplayFor(modelItem => item.FileNo)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
</td>
</tr>
}
</tbody>
</table>
Thank's in advance.
Your ideas is highly appreciated.