private ICollection<File> ManageFiles(QuestionModel model) { var files = new List<File>(); const FileType fileType = FileType.Document; var fileCollection = HttpContext.Current.Request.Files; for (var index = 0; index < fileCollection.Count; index++) { if (fileCollection[index].ContentLength <= 0) { continue; } var uploadConfig = FileHelper.Upload(fileCollection[index], fileType); if (uploadConfig.FileBase == null) continue; var existingRecords = _fileRepository.FindAll(x => x.Question.Id == model.Id).Select(x => new { x.Id, x.RelativePath }).ToList(); /*foreach (var record in existingRecords) { FileHelper.Delete(record.RelativePath); _fileRepository.Delete(record.Id); } _fileRepository.Save();*/ var file = new File { Name = uploadConfig.FileName, MimeType = uploadConfig.FileBase.ContentType, Size = uploadConfig.FileBase.ContentLength, RelativePath = uploadConfig.FilePath + uploadConfig.FileName, FileType = fileType, ObjectState = ObjectState.Added, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow, CreatedBy = HttpContext.Current.User.Identity.GetUserId(), UpdatedBy = HttpContext.Current.User.Identity.GetUserId() }; files.Add(file); } return files; }
private static ICollection<File> ManageFiles(CandidateBase dto) { var files = new List<File>(); var fileCollection = HttpContext.Current.Request.Files; for (var index = 0; index < fileCollection.Count; index++) { if (fileCollection[index].ContentLength <= 0) { continue; } FileType fileType; if (fileCollection[index].FileName == dto.AvatarFileName) { fileType = FileType.Avatar; } else if (fileCollection[index].FileName == dto.ResumeFileName) { fileType = FileType.Resume; } else { fileType = FileType.Document; } var uploadConfig = FileHelper.Upload(fileCollection[index], fileType); if (uploadConfig.FileBase == null) continue; var existingRecords = _fileRepository.FindAll(x => x.Candidate.Id == dto.Id).Select(x => new { x.Id, x.RelativePath }).ToList(); foreach (var record in existingRecords) { FileHelper.Delete(record.RelativePath); _fileRepository.Delete(record.Id); } _fileRepository.Save(); var file = new File { Name = uploadConfig.FileName, MimeType = uploadConfig.FileBase.ContentType, Size = uploadConfig.FileBase.ContentLength, RelativePath = uploadConfig.FilePath + uploadConfig.FileName, FileType = fileType, ObjectState = ObjectState.Added, CreatedAt = DateTime.UtcNow, UpdatedAt = DateTime.UtcNow, CreatedBy = HttpContext.Current.User.Identity.GetUserId(), UpdatedBy = HttpContext.Current.User.Identity.GetUserId() }; files.Add(file); } return files; }
private ICollection<File> ManageFiles(HttpFileCollectionBase fileCollection) { var files = new List<File>(); for (var index = 0; index < fileCollection.Count; index++) { if (fileCollection[index] == null || fileCollection[index].ContentLength <= 0) { continue; } var uploadConfig = UploadFile(Request.Files[index]); if (uploadConfig.FileBase == null) continue; var file = new File { Name = Path.GetFileName(fileCollection[index].FileName), MimeType = uploadConfig.FileBase.ContentType, Size = uploadConfig.FileBase.ContentLength, RelativePath = uploadConfig.FilePath + uploadConfig.FileName, FileType = FileType.Document, CreatedBy = User.Identity.GetUserId(), UpdatedBy = User.Identity.GetUserId() }; files.Add(file); } return files; }
public ActionResult UploadAvatar() { if (Request.Files == null || Request.Files.Count <= 0 || Request.Files[0] == null || Request.Files[0].ContentLength <= 0) { ModelState.AddModelError("", "Invalid file for avatar"); Response.StatusCode = (int) HttpStatusCode.BadRequest; return new JsonResult(ModelState.Values.SelectMany(v => v.Errors)); } var fileBase = Request.Files[0]; var fileName = string.Format("{0}.{1}", Guid.NewGuid(), Path.GetFileName(fileBase.FileName)); FileHelper.SaveFile(new UploadConfig { FileBase = fileBase, FileName = fileName, FilePath = FilePath.AvatarRelativePath }); var userId = User.Identity.GetUserId(); var previousFile = _fileRepository.Find(x => x.ApplicationUserId == userId); if (previousFile != null) { _fileRepository.Delete(_fileRepository.Find(x => x.ApplicationUserId == userId)); var fullPath = Request.MapPath(previousFile.RelativePath); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); } } var file = new File { Name = fileName, MimeType = fileBase.ContentType, Size = fileBase.ContentLength, RelativePath = FilePath.AvatarRelativePath + fileName, FileType = FileType.Avatar, ApplicationUserId = User.Identity.GetUserId(), CreatedBy = User.Identity.GetUserId(), UpdatedBy = User.Identity.GetUserId() }; _fileRepository.Insert(file); _fileRepository.Save(); return new JsonResult(file); }
public async Task<ActionResult> Register(Register model) { if (!ModelState.IsValid) return View(model); var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, PhoneNumber = model.PhoneNumber }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { var role = await _roleManager.FindByIdAsync(model.RoleId); await UserManager.AddToRoleAsync(user.Id, role.Name); if (model.Avatar != null && model.Avatar.ContentLength > 0) { var fileName = $"{Guid.NewGuid()}.{Path.GetFileName(model.Avatar.FileName)}"; FileHelper.SaveFile(new UploadConfig { FileBase = model.Avatar, FileName = fileName, FilePath = FilePath.AvatarRelativePath }); var file = new File { Name = fileName, MimeType = model.Avatar.ContentType, Size = model.Avatar.ContentLength, RelativePath = FilePath.AvatarRelativePath + fileName, FileType = FileType.Avatar, ApplicationUserId = user.Id, CreatedBy = User.Identity.GetUserId(), UpdatedBy = User.Identity.GetUserId() }; _fileRepository.Insert(file); _fileRepository.Save(); } // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); // if role = canidate view () return RedirectToAction("Index", "Account"); } AddErrors(result); // If we got this far, something failed, redisplay form return View(model); }