public async Task UpdateSKUPictures(IList <IFormFile> files, IHostingEnvironment environment) { // Add Pictures to server directory and create SKUPictures var uploads = Path.Combine(environment.WebRootPath, "uploads"); if (files?.Count > 0) { foreach (var formFile in files) { if (formFile.Length <= 0) { continue; } var guid = Guid.NewGuid(); var extension = Path.GetExtension(formFile.FileName); string fileName = $"sku_picture_{guid.ToString()}{extension}"; var imageUrl = Path.Combine("/uploads/", fileName); if (SKUPictures.Any(x => x.ImageUrl == imageUrl)) { continue; //if this image is already saved } using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create)) { await formFile.CopyToAsync(fileStream); } AddSKUPicture(imageUrl, 100); } } }
private void AddSKUPicture(string imageUrl, int sorting) { SKUPictures.Add(new SKUPicture { SKU = this, ImageUrl = imageUrl, Sorting = sorting }); }