`When users upload images in application, they are successfully saved to the server’s file system within a folder named “Images”. However, despite the successful file storage, encountering an issue where the images are not being saved in the database. Notably, this problem is isolated to your live environment running on IIS; my local development environment operates without this issue.
` public async Task<ApprovalPurchaseRequest?> ApproveRequestWithinvoice(int? id, int? decisionNumber, IFormFileCollection? image)
{
var approvalRequest = await _context.ApprovalPurchaseRequests.FindAsync(id);
var purchaseRequest = await _context.PurchaseRequests.FindAsync(approvalRequest?.RequestId);
if (_User.IsInRole("Employee Admin"))
{
string folder = "Images";
string serverFolderPath = Path.Combine(webHost.WebRootPath, folder);
if (image != null && image.Count > 0)
{
foreach (var item in image)
{
if (!Directory.Exists(serverFolderPath))
{
Directory.CreateDirectory(serverFolderPath);
}
string extension = Path.GetExtension(item.FileName);
string fileName = Guid.NewGuid().ToString() + extension;
string filePath = Path.Combine(serverFolderPath, fileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
await item.CopyToAsync(fileStream);
}
string relativeFilePath = "/Images/" + fileName;
Image _image = new()
{
ImagePath = relativeFilePath,
IdPurchaseRequest = purchaseRequest!.IdPurchaseRequest,
ItIsInvoise = true,
};
_context.Add(_image);
await _context.SaveChangesAsync();
}
}
}
return approvalRequest;
}`
`
Ahmad Ali is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.