public async Task <ResponseViewModel> UploadEmissionTestImage(FileContainerModel container, string userName)
        {
            var response = new ResponseViewModel();


            try
            {
                var user = _db.Users.FirstOrDefault(t => t.Username == userName);

                var emissionTestReportRecord = _db.VehicleEmissiontTests.FirstOrDefault(x => x.Id == container.Id);

                var folderPath = emissionTestReportRecord.GetVehicleEmissionTestFolderPath(config);

                if (!string.IsNullOrEmpty(emissionTestReportRecord.Attachment))
                {
                    var existingImagePath = string.Format(@"{0}\{1}", folderPath, emissionTestReportRecord.Attachment);

                    if (File.Exists(existingImagePath))
                    {
                        File.Delete(existingImagePath);
                    }
                }

                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }

                var firstFile = container.Files.FirstOrDefault();
                if (firstFile != null && firstFile.Length > 0)
                {
                    var fileName = emissionTestReportRecord.GetVehicleEmissionTestImageName(Path.GetExtension(firstFile.FileName));
                    var filePath = string.Format(@"{0}\{1}", folderPath, fileName);
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await firstFile.CopyToAsync(stream);

                        emissionTestReportRecord.Attachment = fileName;

                        _db.VehicleEmissiontTests.Update(emissionTestReportRecord);

                        await _db.SaveChangesAsync();
                    }
                }

                response.IsSuccess = true;
                response.Message   = "Emission Test Report image has been uploaded succesfully.";
            }
            catch (Exception ex)
            {
                logger.LogError(ex.ToString());
                response.IsSuccess = false;
                response.Message   = "Error has been occured while uploading the file. Please try again.";
            }

            return(response);
        }
        public MyForm()
        {
            FileContainer = new FileContainerModel();

            //CompareResult = new Dictionary<string, int>();

            InitializeComponent();

            advOcr = SetOcrAdvConfig();
        }
        public async Task <IActionResult> UploadEmissionTestImage()
        {
            var userName = identityService.GetUserName();

            var container = new FileContainerModel();

            var request = await Request.ReadFormAsync();

            container.Id = int.Parse(request["id"]);

            foreach (var file in request.Files)
            {
                container.Files.Add(file);
            }

            var response = await _vehicleEmissionTestService.UploadEmissionTestImage(container, userName);

            return(Ok(response));
        }
        public async Task <ResponseViewModel> UploadUserImage(FileContainerModel container, string userName)
        {
            var response = new ResponseViewModel();


            try
            {
                var user       = _db.Users.FirstOrDefault(t => t.Username == userName);
                var userRecord = _db.Users.FirstOrDefault(x => x.Id == container.Id);
                //var folderPath = string.Empty;
                //var fileName = string.Empty;
                //var filePath = string.Empty;

                var firstFile = container.Files.FirstOrDefault();

                switch (container.Type)
                {
                case (int)UserPhotoType.UserImage:
                {
                    var folderPath = userRecord.GetUserImagePath(_config);
                    if (!string.IsNullOrEmpty(userRecord.Image))
                    {
                        var existingImagePath = string.Format(@"{0}\{1}", folderPath, userRecord.Image);
                        if (File.Exists(existingImagePath))
                        {
                            File.Delete(existingImagePath);
                        }
                    }

                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }

                    if (firstFile != null && firstFile.Length > 0)
                    {
                        var fileName = userRecord.GetUserImageName(Path.GetExtension(firstFile.FileName));
                        var filePath = string.Format(@"{0}\{1}", folderPath, fileName);
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            await firstFile.CopyToAsync(stream);

                            userRecord.Image = fileName;

                            _db.Users.Update(userRecord);

                            await _db.SaveChangesAsync();

                            response.Message = "User image has been uploaded succesfully.";
                        }
                    }
                }
                break;

                case (int)UserPhotoType.NicBack:
                {
                    var folderPath = userRecord.GetUserNICBackImagePath(_config);
                    if (!string.IsNullOrEmpty(userRecord.NicbackImage))
                    {
                        var existingImagePath = string.Format(@"{0}\{1}", folderPath, userRecord.NicbackImage);
                        if (File.Exists(existingImagePath))
                        {
                            File.Delete(existingImagePath);
                        }
                    }

                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }

                    if (firstFile != null && firstFile.Length > 0)
                    {
                        var fileName = userRecord.GetNICBackImageName(Path.GetExtension(firstFile.FileName));
                        var filePath = string.Format(@"{0}\{1}", folderPath, fileName);
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            await firstFile.CopyToAsync(stream);

                            userRecord.Image = fileName;

                            _db.Users.Update(userRecord);

                            await _db.SaveChangesAsync();

                            response.Message = "User NIC back image has been uploaded succesfully.";
                        }
                    }
                }
                break;

                case (int)UserPhotoType.NicFront:
                {
                    var folderPath = userRecord.GetUserNICFrontImagePath(_config);
                    if (!string.IsNullOrEmpty(userRecord.NicfrontImage))
                    {
                        var existingImagePath = string.Format(@"{0}\{1}", folderPath, userRecord.NicfrontImage);
                        if (File.Exists(existingImagePath))
                        {
                            File.Delete(existingImagePath);
                        }
                    }

                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }

                    if (firstFile != null && firstFile.Length > 0)
                    {
                        var fileName = userRecord.GetNICFrontImageName(Path.GetExtension(firstFile.FileName));
                        var filePath = string.Format(@"{0}\{1}", folderPath, fileName);
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            await firstFile.CopyToAsync(stream);

                            userRecord.Image = fileName;

                            _db.Users.Update(userRecord);

                            await _db.SaveChangesAsync();

                            response.Message = "User NIC front image has been uploaded succesfully.";
                        }
                    }
                }
                break;

                case (int)UserPhotoType.DrivingLicenceFront:
                {
                    var folderPath = userRecord.GetUserDrivingLicenceFrontImagePath(_config);
                    if (!string.IsNullOrEmpty(userRecord.DrivingLicenceFrontImage))
                    {
                        var existingImagePath = string.Format(@"{0}\{1}", folderPath, userRecord.DrivingLicenceFrontImage);
                        if (File.Exists(existingImagePath))
                        {
                            File.Delete(existingImagePath);
                        }
                    }

                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }

                    if (firstFile != null && firstFile.Length > 0)
                    {
                        var fileName = userRecord.GetDrivingLicenceFrontImageName(Path.GetExtension(firstFile.FileName));
                        var filePath = string.Format(@"{0}\{1}", folderPath, fileName);
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            await firstFile.CopyToAsync(stream);

                            userRecord.Image = fileName;

                            _db.Users.Update(userRecord);

                            await _db.SaveChangesAsync();

                            response.Message = "User driving licence front image has been uploaded succesfully.";
                        }
                    }
                }
                break;

                case (int)UserPhotoType.DrivingLicenceBack:
                {
                    var folderPath = userRecord.GetUserDrivingLicenceBackImagePath(_config);
                    if (!string.IsNullOrEmpty(userRecord.DrivingLicenceFrontImage))
                    {
                        var existingImagePath = string.Format(@"{0}\{1}", folderPath, userRecord.DrivingLicenceBackImage);
                        if (File.Exists(existingImagePath))
                        {
                            File.Delete(existingImagePath);
                        }
                    }

                    if (!Directory.Exists(folderPath))
                    {
                        Directory.CreateDirectory(folderPath);
                    }

                    if (firstFile != null && firstFile.Length > 0)
                    {
                        var fileName = userRecord.GetDrivingLicenceBackImageName(Path.GetExtension(firstFile.FileName));
                        var filePath = string.Format(@"{0}\{1}", folderPath, fileName);
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            await firstFile.CopyToAsync(stream);

                            userRecord.Image = fileName;

                            _db.Users.Update(userRecord);

                            await _db.SaveChangesAsync();

                            response.Message = "User driving licence back image has been uploaded succesfully.";
                        }
                    }
                }
                break;
                }

                response.IsSuccess = true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                response.IsSuccess = false;
                response.Message   = "Error has been occured while uploading the file. Please try again.";
            }

            return(response);
        }