/** * Removes the to be deleted users profile image from the blob storage to conserve storage room */ private async Task DeleteUserBlobImage(string imageUrl) { var blobStorageContainer = _uploadService.GetStorageContainer(AzureBlobStorageConnection); // Service client is used to get the reference because we have the Uri of the image not its name within the // container var blobImage = await blobStorageContainer.ServiceClient.GetBlobReferenceFromServerAsync(new Uri(imageUrl)); await blobImage.DeleteAsync(); }
private CloudBlockBlob UploadForumImage(IFormFile file) { var container = _uploadService.GetStorageContainer(AzureBlobStorageConnection); var contentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition); var fileName = contentDisposition.FileName.Trim('"'); var blockBlob = container.GetBlockBlobReference(fileName); // Do not need await modifier in this case blockBlob.UploadFromStreamAsync(file.OpenReadStream()); return(blockBlob); }
public async Task <IActionResult> UploadUserProfileImage(IFormFile file) { var userId = _userManager.GetUserId(User); var container = _uploadService.GetStorageContainer(AzureBlobStorageConnection); var contentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition); var fileName = contentDisposition.FileName.Trim('"'); var blockBlob = container.GetBlockBlobReference(fileName); await blockBlob.UploadFromStreamAsync(file.OpenReadStream()); await _userService.SetProfileImageAsync(userId, blockBlob.Uri); return(RedirectToAction("UserDetails", "UserProfile", new { userId })); }
/* * Uploads User Profile image to Azure Blob storage */ private async Task UploadUserProfileImage(IFormFile file) { var userId = _userManager.GetUserId(User); var container = _uploadService.GetStorageContainer(AzureBlobStorageConnection); // Get file name var contentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition); var fileName = contentDisposition.FileName.Trim('"'); // Replace it with userId to save space in Azure blob // As the file with the same name will overrite the old file. var fileExtension = fileName.Substring(fileName.LastIndexOf('.')); var userIdFileName = String.Concat(userId, fileExtension); var blockBlob = container.GetBlockBlobReference(userIdFileName); await blockBlob.UploadFromStreamAsync(file.OpenReadStream()); await _applicationUserService.SetProfileImageAsync(userId, blockBlob.Uri); }