// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { ModelState.AddModelError("", "One or More Fields is invalid"); return(Page()); } var ModelId = Cars.CarsModelId; var ModelName = _context.CarsModel.FindAsync(ModelId).Result; if (ModelName == null) { return(NotFound()); } if (FileUpload != null) { UploadFolder = "upload"; WebRoot = _hostingEnvironment.WebRootPath; UploadDir = System.IO.Path.Combine(WebRoot, UploadFolder); if (!Directory.Exists(UploadDir)) { Directory.CreateDirectory(UploadDir); } //long size = FileUpload.Sum(f => f.Length); //Home file upload NewString = CreateRandomString(); NewFileString = CreateRandomString(); FileName = ContentDispositionHeaderValue.Parse(FileUpload.ContentDisposition).FileName.Trim('"'); Directory.CreateDirectory(UploadDir + "/" + NewString); DynamicImageFolder = System.IO.Path.Combine(UploadDir, NewString); //var ImageFolder = UploadDir + "/" + NewString; // store final final filename string FullPath = Path.Combine(DynamicImageFolder, FileName); // create dynamic string for unique folder FileType = System.IO.Path.GetExtension(FileName); FileNameRenamed = NewFileString + FileType; if (FileUpload.Length > 0) { using (var stream = new FileStream(FullPath, FileMode.Create)) { FileUpload.CopyTo(stream); ResizeToFixedSize(FullPath, FileName); //crop the uploaded file and rename it System.IO.FileInfo fi = new System.IO.FileInfo(FullPath + FileName); // access the cropped image directory fi.MoveTo(UploadDir + "/" + NewString + "/" + NewFileString + FileType); //fi.Delete(FullPath+FileName); } } } if (MultipleFileUpload.Count > 0 && MultipleFileUpload != null) { NewThumbString = CreateRandomString(); Directory.CreateDirectory(UploadDir + "/" + NewThumbString + "/" + "thumbs"); // save Car data to DB Cars.ModelName = ModelName.Name; // Map Cars ModelName Object to CarsModel Name Object Cars.Year = ModelName.Year; Cars.UploadedDate = DateTime.Now; Cars.DisplayFileName = FileNameRenamed; Cars.FolderName = NewString; Cars.ThumbFolderName = NewThumbString; Cars.ArrayFileName = "ArrFileName"; // _context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT [dbo].[CarImages] ON"); _context.Add(Cars); await _context.SaveChangesAsync(); //retrieve last inserted record; int counter = 0; foreach (IFormFile ModelFile in MultipleFileUpload) //Home file upload { ArrFileName = ContentDispositionHeaderValue.Parse(ModelFile.ContentDisposition).FileName.Trim('"'); ThumbImageFolder = System.IO.Path.Combine(UploadDir, NewThumbString, "thumbs"); ArrFileName = GetUniqueFileName(ArrFileName); string FullPath = Path.Combine(ThumbImageFolder, ArrFileName); // create dynamic string for unique folder //FileType = System.IO.Path.GetExtension(FileName); if (MultipleFileUpload.Count > 0) { using (var transaction = _context.Database.BeginTransaction()) { using (var stream = new FileStream(FullPath, FileMode.Create)) { ModelFile.CopyTo(stream); CarImages.ImagesName = ArrFileName; CarImages.FolderName = NewThumbString; CarImages.CarsId = GetLastCar().Id; CarImages.CreatedDate = DateTime.Now; CarImages.Cid = AutoId(); _context.Add(CarImages); await _context.SaveChangesAsync(); transaction.CommitAsync(); } } } counter++; } // _context.Remove(GetLastCar()); // await _context.SaveChangesAsync(); Message = "Successful;"; return(RedirectToPage("./Index")); } return(this.Content("Failed")); }