internal void SetupClickEventsForPage(string pageTitle, HtmlElement activeElement, HtmlDocument currentDocument) { switch (pageTitle) { case "default": break; case "items": break; case "uploadfile": if (activeElement.Id != null && activeElement.Id.Equals("uploaded_file")) { HtmlElement filePath = currentDocument.GetElementById("filepath"); if (filePath != null && filePath.InnerText != null) { // File Ext and file name var file = _fileHelper.GetFile(filePath.InnerText); List <Item> validItems = new List <Item>(); if (_fileValidation.ValidateFile(file, _fileHelper, validItems)) { var fileName = file.Filename.Split('\\').Last(); _dbHelper.AddFileToDatabase(_connectionString, $"{_fileHelper.GetFileName(filePath.InnerText)}", file.FileBytes); filePath.InnerText = ""; MessageBox.Show("File Successfully Uploaded"); // GetFileId foreach (var item in validItems) { _dbHelper.AddItemToDatabase(item, fileName); } DisplayLoadedItems(currentDocument); } else { MessageBox.Show("Invalid File, Please see logs"); filePath.InnerText = null; } } } break; case "logfile": if (activeElement.Id != null && activeElement.Id.Equals("fetchdata")) { GetLogFiles(currentDocument); } break; } }
public override ResultModel <Guid> AddFile(UploadFileViewModel dto, Guid tenantId) { var fileValidation = FileValidation.ValidateFile(dto.File, _options.Value.FirstOrDefault(x => x.TenantId == tenantId)); if (!fileValidation.IsSuccess) { return(fileValidation); } if (dto.Id != Guid.Empty) { return(UpdateFile(dto)); } var encryptedFile = SaveFilePhysical(dto.File); if (encryptedFile == null) { return(ExceptionMessagesEnum.NullIFormFile.ToErrorModel <Guid>()); } var file = new FileBox { FileExtension = encryptedFile.FileExtension, Path = encryptedFile.Path, Name = encryptedFile.FileName, Size = encryptedFile.Size }; _context.FilesBox.Add(file); _context.SaveChanges(); return(new ResultModel <Guid> { IsSuccess = true, Result = file.Id }); }
private ResultModel <Guid> UpdateFile(UploadFileViewModel dto, Guid tenantId) { var file = _context.Files.FirstOrDefault(x => x.Id == dto.Id); if (file == null) { return(ExceptionMessagesEnum.FileNotFound.ToErrorModel <Guid>()); } var fileValidation = FileValidation.ValidateFile(dto.File, _options.Value.FirstOrDefault(x => x.TenantId == tenantId)); if (!fileValidation.IsSuccess) { return(fileValidation); } var encryptedFile = EncryptFile(dto.File); if (encryptedFile == null) { return(ExceptionMessagesEnum.NullIFormFile.ToErrorModel <Guid>()); } file.FileExtension = encryptedFile.FileExtension; file.Hash = encryptedFile.EncryptedFile; file.Name = encryptedFile.FileName; file.Size = encryptedFile.Size; _context.Files.Add(file); _context.SaveChanges(); return(new ResultModel <Guid> { IsSuccess = true, Result = file.Id }); }
public IActionResult Create(IFormFile[] files) { bool AllValid = true; string inValid = ""; if (files[0] != null) { if (files.Length <= 10) { foreach (IFormFile file in files) { if (!validation.ValidateFile(file)) { AllValid = false; inValid += ", " + file.FileName; } } if (AllValid) { foreach (IFormFile file in files) { try { validation.SaveFileToDisk(file); } catch (Exception) { ModelState.AddModelError("FileName", "Sorry an error occurred saving the files to disk, please try again"); } } } else { ModelState.AddModelError("FileName", "All files must be gif, png, jpeg or jpg and less than 2MB in size.The following files" + inValid + " are not valid"); } } else { ModelState.AddModelError("FileName", "Please only upload up to ten files at a time"); } } else { ModelState.AddModelError("FileName", "Please choose a file"); } if (ModelState.IsValid) { bool duplicates = false; bool otherDbError = false; string duplicateFiles = ""; foreach (IFormFile file in files) { ProductImage productImage = new ProductImage { FileName = file.FileName }; try { repository.Add(productImage); repository.Save(); } catch (Exception e) { duplicateFiles += ", " + file.FileName; duplicates = true; repository.Detache(productImage); } } if (duplicates) { ModelState.AddModelError("FileName", "All files uploaded except the files" + duplicateFiles + ", which already exist in the system." + " Please delete them and try again if you wish to re - add them"); return(View()); } return(RedirectToAction(nameof(Index))); } return(View()); }