private string ProcessUploadedFile(TourPutDTO model) { string uniqueFileName = null; if (model.Image != null) { string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "TourImages"); uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Image.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.Image.CopyTo(fileStream); } } return(uniqueFileName); }
public async Task <string> ProcessTourImgAsync(TourPutDTO model) { string uniqueFileName = null; if (model.Image != null) { uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Image.FileName; // Retrieve storage account from connection string. CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(StorageConnectionString); // Create the blob client. CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient(); // Retrieve a reference to a container. CloudBlobContainer container = blobClient.GetContainerReference("tourimages"); // This also does not make a service call; it only creates a local object. CloudBlockBlob blockBlob = container.GetBlockBlobReference(uniqueFileName); await using (var data = model.Image.OpenReadStream()) { await blockBlob.UploadFromStreamAsync(data); } } return(uniqueFileName); }
public async Task <IActionResult> PutTour(int id, [FromForm] TourPutDTO model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var tour = await _context.Tours.FindAsync(id); if (tour == null) { ModelState.AddModelError("", $"Tour with id : {id} not found"); return(NotFound(ModelState)); } if (model.Image != null) { if (tour.Image != null) { //string filePath = Path.Combine(webHostEnvironment.WebRootPath, // "TourImages", tour.Image); //System.IO.File.Delete(filePath); await _AzureFileService.DeleteTourImageAsync(tour.Image); } //tour.Image = ProcessUploadedFile(model); tour.Image = await _AzureFileService.ProcessTourImgAsync(model); } tour.City = model.City; tour.Coordinates = model.Coordinates; tour.Country = model.Country; tour.Description = model.Description; tour.Latitude = model.Latitude; tour.Longitude = model.Longitude; tour.Name = model.Name; tour.Province = model.Province; tour.Region = model.Region; tour.Type = model.Type; _context.Entry(tour).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TourExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }