public IActionResult PDFDataUpload(PDFUploadViewModel model, int fileId, string category) { //error catcher. If the user clicked the "upload" button without selecting a file, return to them the same page, and pass the category and fileId so those don't get los if (model.PDFFiles == null) { ViewBag.fileId = fileId; ViewBag.category = category; return(View()); } //save the file the user wants to upload to the correct place on the server if (ModelState.IsValid) { string uniqueFileName = null; if (model.PDFFiles != null) { string uploadsFolder = null; //create the correct path to save the file based on which accordian box the user clicked on if (fileId == 1) { uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "pdf/Osteology Data"); } else if (fileId == 2) { uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "pdf/Field Books"); } else if (fileId == 3) { uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "pdf/Archaeological Database Articles"); } else { uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "pdf/Misc Data"); } uniqueFileName = Guid.NewGuid().ToString() + "_" + model.PDFFiles.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); model.PDFFiles.CopyTo(new FileStream(filePath, FileMode.Create)); } //create a new pdf file object. Save the path, name, and FileId PDFFile newPDFFile = new PDFFile { Path = uniqueFileName, Name = model.PDFFiles.FileName, FileId = fileId }; //add the object to the database and save it _context.PDFFiles.Add(newPDFFile); _context.SaveChanges(); //return the user to the PDF data page with the context file return(RedirectToAction("PDFData", _context.PDFFiles)); } return(View()); }
public ActionResult Create() { var classes = _classService.GetClasses().ToList(); var pdfcategories = _pdfCategoryService.GetPDFCategories().ToList(); var viewModel = new PDFUploadViewModel(); viewModel.Classes = new SelectList(classes, "ClassId", "Name"); viewModel.PDFCategories = new SelectList(pdfcategories, "PDFCategoryId", "Name"); return(View(viewModel)); }
public ActionResult Create(PDFUploadViewModel viewModel) { var cmsResult = new CMSResult(); if (ModelState.IsValid) { if (viewModel.FilePath.ContentLength == 0) { cmsResult.Results.Add(new Result { Message = "Pdf is required", IsSuccessful = false }); } if (!Common.Constants.PdfType.Contains(viewModel.FilePath.ContentType)) { cmsResult.Results.Add(new Result { Message = "Please choose pdf file.", IsSuccessful = false }); _logger.Warn(cmsResult.Results.FirstOrDefault().Message); Warning(cmsResult.Results.FirstOrDefault().Message, true); } if (cmsResult.Success) { var pdfupload = new PDFUpload { ClassId = viewModel.ClassId, Title = viewModel.Title, FileName = viewModel.FilePath.FileName, IsVisible = viewModel.IsVisible, PDFCategoryId = viewModel.PDFCategoryId, IsSend = viewModel.IsSend }; var result = _pdfUploadService.Save(pdfupload); var pdfuploadId = pdfupload.PDFUploadId; if (result.Success) { string folderPath = Server.MapPath(string.Concat("~/PDF/", Common.Constants.PdfFileFolder)); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } var pathToSaveQI = Path.Combine(folderPath, string.Format("{0}", viewModel.FilePath.FileName)); if (viewModel.FilePath != null) { viewModel.FilePath.SaveAs(pathToSaveQI); } var bodySubject = "Web portal - PDF Upload Created"; var message = "PDFUpload Created Successfully"; SendMailToAdmin(message, viewModel.Title, viewModel.PDFCategoryName, viewModel.ClassName, bodySubject); var successMessage = result.Results.FirstOrDefault().Message; CMSResult sendNotificationResult = new CMSResult(); if (viewModel.IsSend) { sendNotificationResult = SendNotification(viewModel.ClassId, viewModel.Title, viewModel.FilePath.FileName, viewModel.PDFCategoryName, pdfuploadId); successMessage += " <br/>" + sendNotificationResult.Results.FirstOrDefault().Message; } Success(successMessage); ModelState.Clear(); viewModel = new PDFUploadViewModel(); } else { _logger.Warn(result.Results.FirstOrDefault().Message); Warning(result.Results.FirstOrDefault().Message, true); } } } else { cmsResult.Results.Add(new Result { Message = "Please select PDF!", IsSuccessful = true }); } var classes = _classService.GetClasses().ToList(); var pdfcategories = _pdfCategoryService.GetPDFCategories().ToList(); viewModel.Classes = new SelectList(classes, "ClassId", "Name"); viewModel.PDFCategories = new SelectList(pdfcategories, "PDFCategoryId", "Name"); return(View(viewModel)); }