public async Task <IActionResult> Edit(int id, [Bind("Id,ImgUrl")] EventImg eventImg) { if (id != eventImg.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(eventImg); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EventImgExists(eventImg.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(eventImg)); }
public async Task <IActionResult> Create([Bind("Id,ImgUrl")] EventImg eventImg) { if (ModelState.IsValid) { _context.Add(eventImg); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(eventImg)); }
public async Task <bool> UploadFileToStorage(Stream fileStream, string fileName, AzureStorageConfig _storageConfig, [Bind("id,ImgUrl")] EventImg eventImg, [Bind("Id,EventName,EventDate,Description,Place,AforoActual,AforoTotal,Visitas")] Evento evento, string time) { // Create storagecredentials object by reading the values from the configuration (appsettings.json) StorageCredentials storageCredentials = new StorageCredentials(_storageConfig.AccountName, _storageConfig.AccountKey); // Create cloudstorage account by passing the storagecredentials CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true); // Create the blob client. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Get reference to the blob container by passing the name by reading the value from the configuration (appsettings.json) CloudBlobContainer container = blobClient.GetContainerReference(_storageConfig.EventImgContainer); // Get the reference to the block blob from the container CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); // Upload the file await blockBlob.UploadFromStreamAsync(fileStream); var blobUrl = blockBlob.Uri.AbsoluteUri; var img = new EventImg { ImgUrl = blobUrl }; _context.Add(img); await _context.SaveChangesAsync(); DateTimeOffset eventodate = evento.EventDate; var timeSpanVal = time.ToString().Split(':').Select(x => Convert.ToInt32(x)).ToList(); TimeSpan ts = new TimeSpan(timeSpanVal[0], timeSpanVal[1], 00); evento.EventDate = eventodate.Add(ts); evento.Estado = _context.EstadoEventos.Single(x => x.Id == 1); evento.Imgs = img; evento.AforoActual = 0; evento.Visitas = 0; if (ModelState.IsValid) { _context.Add(evento); await _context.SaveChangesAsync(); } return(await Task.FromResult(true)); }
public async Task <IActionResult> Post(List <IFormFile> files, [Bind("id,ImgUrl")] EventImg eventImg, [Bind("Id,EventName,EventDate,Description,Place,AforoActual,AforoTotal,Visitas")] Evento evento, string time) { var uploadSuccess = false; foreach (var formFile in files) { if (formFile.Length <= 0) { continue; } // NOTE: uncomment either OPTION A or OPTION B to use one approach over another // OPTION A: convert to byte array before upload //using (var ms = new MemoryStream()) //{ // formFile.CopyTo(ms); // var fileBytes = ms.ToArray(); // uploadSuccess = await UploadToBlob(formFile.FileName, fileBytes, null); //} // OPTION B: read directly from stream for blob upload using (var stream = formFile.OpenReadStream()) { uploadSuccess = await UploadFileToStorage(stream, formFile.FileName, _storageConfig, eventImg, evento, time); } } if (uploadSuccess) { return(RedirectToAction("Create", "Eventos")); } else { return(View("UploadError")); } }