public IActionResult Add(vmCalculator vmc, IFormFile img) { Calculator calculator = vmc.Calc; calculator.ImagePath = vmc.Image.FileName; calculator.SellerId = 1; var calc = _context.Calculators.Add(calculator); SaveDocuments(vmc); _context.SaveChanges(); return(RedirectToAction("Index")); }
bool SaveDocuments(vmCalculator patient) { List <IFormFile> docFiles = new List <IFormFile> { patient.Image }; if (docFiles?.Count > 0) { string webRootPath = _hostingEnvironment.WebRootPath; string contentRootPath = _hostingEnvironment.ContentRootPath; //var _DestinationDirectory = @"/UploadedFiles/Documents/"; var _DestinationDirectory = "wwwroot/images/"; //var _NewDirectory = Guid.NewGuid(); //_DestinationDirectory = _DestinationDirectory + _NewDirectory; try { foreach (var supportingDocument in docFiles) { var _NewFile = string.Empty; var _FullPath = string.Empty; var _NewFileExtension = Path.GetExtension(supportingDocument.FileName); // var _NewFileName = Path.ChangeExtension(Path.GetRandomFileName(), _NewFileExtension); _FullPath = _DestinationDirectory + @"/" + supportingDocument.FileName; var exists = Directory.Exists(_DestinationDirectory); //if (!exists) // Directory.CreateDirectory(webRootPath + _DestinationDirectory); using (FileStream fs = System.IO.File.Create(_FullPath)) { supportingDocument.CopyTo(fs); fs.Flush(); } _NewFile = string.Empty; _FullPath = string.Empty; } _context.SaveChanges(); return(true); } catch (Exception ex) { goto ExitMethod; } } ExitMethod: return(false); }