public async Task<ActionResult> Create(SpecimenPhotoUploadModel model) { if (ModelState.IsValid) { var returnModel = new DigitalPhotographPostUploadModel(); foreach (var photo in model.Photos) { var fileName = Guid.NewGuid().ToString() + "_" + photo.FileName; var dPhoto = new DigitalPhotograph { SpecimenId = model.SpecimenId, FileName = fileName }; // Save the file and the smaller versions. var completePath = Server.MapPath("~/App_Data/Uploads/Photos/"); Directory.CreateDirectory(completePath); photo.SaveAs(completePath + fileName); FileUploadHelper.GenerateVersions(completePath + fileName); returnModel.UploadedItems.Add(new DigitalPhotographUploadItem { DigitalPhotograph = dPhoto }); db.DigitalPhotographs.Add(dPhoto); } await db.SaveChangesAsync(); if (Request.IsAjaxRequest()) { return PartialView("PostUpload", returnModel); } return View("PostUpload", returnModel); } return View(model); }
public async Task<ActionResult> PostUpload(DigitalPhotographPostUploadModel model) { if (ModelState.IsValid) { foreach (var item in model.UploadedItems) { var photo = await db.DigitalPhotographs.FindAsync(item.DigitalPhotograph.Id); if (!item.Save) { // Remove the picture and its files. db.DigitalPhotographs.Remove(photo); var fullPath = Server.MapPath("~/App_Data/Uploads/Photos/" + photo.FileName); // Remove file from the disk. if (System.IO.File.Exists(fullPath + "_thumb.jpg")) { System.IO.File.Delete(fullPath + "_thumb.jpg"); } if (System.IO.File.Exists(fullPath + "_large.jpg")) { System.IO.File.Delete(fullPath + "_large.jpg"); } } } await db.SaveChangesAsync(); return RedirectToAction("Details", "Specimens", new { id = model.UploadedItems[0].DigitalPhotograph.SpecimenId }); } return View(model); }