public async Task <IActionResult> Create(PDF_Documents pDF_Documents) { if (ModelState.IsValid) { if (pDF_Documents.Document != null) { string docfolder = "PDF_Documents/" + Guid.NewGuid().ToString() + pDF_Documents.Document.FileName; string serverFolder = Path.Combine(_webHostEnvironment.WebRootPath, docfolder); await pDF_Documents.Document.CopyToAsync(new FileStream(serverFolder, FileMode.Create)); //model.SaveAs(Server.MapPath(filePath)); //var filename = Path.GetFileName(file.FileName); //var path = Path.Combine(Server.MapPath("~/Uploads/Photo/"), filename); //file.SaveAs(path); //tyre.Url = filename; if (pDF_Documents.Cover_image != null) { string coverfolder = "Cover_images/" + Guid.NewGuid().ToString() + pDF_Documents.Cover_image.FileName; string serverFolder1 = Path.Combine(_webHostEnvironment.WebRootPath, coverfolder); await pDF_Documents.Cover_image.CopyToAsync(new FileStream(serverFolder1, FileMode.Create)); PDF_Documents pdfdoc = new PDF_Documents { Document_name = pDF_Documents.Document_name, Document_description = pDF_Documents.Document_description, Book_url = docfolder, Cover_url = coverfolder }; _context.Add(pdfdoc); await _context.SaveChangesAsync(); TempData["book"] = docfolder; TempData["name"] = pDF_Documents.Document_name; TempData["description"] = pDF_Documents.Document_description; TempData["cover"] = coverfolder; } } //_context.Add(pDF_Documents); //await _context.SaveChangesAsync(); //return RedirectToAction(nameof(Index)); } //var itemlist = _context.PDF_Documents.Where(x => x.ID==pDF_Documents.ID).ToList(); return(View(pDF_Documents)); }
public async Task <IActionResult> Edit(int id, PDF_Documents pDF_Documents) { if (id != pDF_Documents.ID) { return(NotFound()); } if (ModelState.IsValid) { try { int idd = id; var pDF_Documents0 = await _context.PDF_Documents.FindAsync(id); var pDF_Documents1 = await _context.PDF_Documents.FindAsync(idd); var pDF_Documents2 = await _context.PDF_Documents.FindAsync(id); GC.Collect(); GC.WaitForPendingFinalizers(); var path = Path.Combine(_webHostEnvironment.WebRootPath, pDF_Documents2.Book_url); System.IO.File.Delete(path); GC.Collect(); GC.WaitForPendingFinalizers(); string docfolder = "PDF_Documents/" + Guid.NewGuid().ToString() + pDF_Documents.Document.FileName; string serverFolder = Path.Combine(_webHostEnvironment.WebRootPath, docfolder); await pDF_Documents.Document.CopyToAsync(new FileStream(serverFolder, FileMode.Create)); GC.Collect(); GC.WaitForPendingFinalizers(); var coverPath = Path.Combine(_webHostEnvironment1.WebRootPath, pDF_Documents1.Cover_url); System.IO.File.Delete(coverPath); GC.Collect(); GC.WaitForPendingFinalizers(); string coverfolder = "Cover_images/" + Guid.NewGuid().ToString() + pDF_Documents.Cover_image.FileName; string serverFolder1 = Path.Combine(_webHostEnvironment.WebRootPath, coverfolder); await pDF_Documents.Cover_image.CopyToAsync(new FileStream(serverFolder1, FileMode.Create)); var entity = _context.PDF_Documents.FirstOrDefault(item => item.ID == id); if (entity != null) { entity.Book_url = docfolder; entity.Cover_url = coverfolder; entity.Date_modified = DateTime.Now.ToString(); entity.Document_description = pDF_Documents.Document_description; entity.Document_name = pDF_Documents.Document_name; await _context.SaveChangesAsync(); TempData["status"] = pDF_Documents.Document_name + " has been updated successfully!"; } } catch (DbUpdateConcurrencyException) { if (!PDF_DocumentsExists(pDF_Documents.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(pDF_Documents)); }