// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. // https://docs.microsoft.com/en-us/azure/storage/blobs/storage-upload-process-images?tabs=dotnet public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Cat.Add(Cat); await _context.SaveChangesAsync(); foreach (var item in FormFileToBeUploaded) { using (Stream stream = item.OpenReadStream()) { StorageCredentials cred = new StorageCredentials(Secrets.storageName2, Secrets.storageKey2); Uri url = new Uri(Secrets.imageContainer2); // The following container was premade, but if it wasn't, there could be a check for if container exists CloudBlobContainer container = new CloudBlobContainer(url, cred); if (!container.Exists()) { return(RedirectToPage("./Index")); // Create the container? // In production it would always be create container for the images belonging to that listing } string fileName = "Testing" + item.FileName; CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName); await blockBlob.UploadFromStreamAsync(stream); } } return(RedirectToPage("./Index")); }
// To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(Country).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CountryExists(Country.Id)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
} // The uploaded file has to be a separate property // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { //This is the important step to make a file identifiable Country.FileName = "Test" + FormFileToBeUploaded.FileName; // A simple test to see if I can extract the name of the uploaded file and apply it to the model's FileName //Need to also remember a container name //https://github.com/Azure/azure-sdk-for-net/blob/6e51ae9d74b2afebd02b253eaf82b18082a78da9/sdk/storage/Azure.Storage.Blobs/README.md if (!ModelState.IsValid) { return(Page()); } _context.Country.Add(Country); await _context.SaveChangesAsync(); // In production, create a new container for each person who uploads, or each new listing, and remember this name for each listing/person //BlobServiceClient blobServiceClient = new BlobServiceClient(Secrets.connectionString); //string containerName = "UniqueContainerName"; // Give it a name: "MyContainer1" could be specific to a user, which it probably should //(BlobContainerClient containerClient =) await blobServiceClient.CreateBlobContainerAsync(containerName); //BlobContainerClient containerClient = new BlobContainerClient(Secrets.connectionString, "images"); BlobClient blobClient = new BlobClient(connectionString: Secrets.connectionString2, blobContainerName: "images", blobName: Country.FileName); //FormFileToBeUploaded.ContentType = "image/png"; using Stream uploadFileStream = FormFileToBeUploaded.OpenReadStream(); await blobClient.UploadAsync(uploadFileStream); uploadFileStream.Close(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Cat = await _context.Cat.FindAsync(id); if (Cat != null) { _context.Cat.Remove(Cat); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }