public async Task <IActionResult> OnPost(IFormCollection form) { if (ModelState.IsValid) { var file = form.Files.FirstOrDefault(); using (Stream stream = file.OpenReadStream()) { // Create a URI to the blob String stringUrl = "https://" + _storageConfig.AccountName + ".blob.core.windows.net/" + _storageConfig.ImageContainer + "/" + file.FileName; Uri blobUri = new Uri(stringUrl); // Create StorageSharedKeyCredentials object by reading // the values from the configuration (appsettings.json) StorageSharedKeyCredential storageCredentials = new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey); // Create the blob client. BlobClient blobClient = new BlobClient(blobUri, storageCredentials); // Upload the file await blobClient.UploadAsync(stream); AnimalsLost.ImageUrl = stringUrl; } if (User.Identity.IsAuthenticated) { AnimalsLost.UserId = _userManager.GetUserId(HttpContext.User); } await _db.AnimalsLost.AddAsync(AnimalsLost); await _db.SaveChangesAsync(); //COMMENT:if we got this far, it was successful so let's tell the user. //set a tempData variable to a success string. we will use this //variable after the redirect to the gallery. TempData["SuccessMessage"] = "Image upload success!"; return(RedirectToPage("Lost")); } else { return(Page()); } }
public async Task <IActionResult> OnPost() { if (ModelState.IsValid) { await _db.AnimalsLost.AddAsync(Animal); await _db.SaveChangesAsync(); return(RedirectToPage("AnimalList")); } else { return(Page()); } }