Created
February 2, 2023 04:25
-
-
Save Dark-Rex01/60e459e395a1f2be67ca156a9fa1063b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<string> Upload(IFormFile files) | |
{ | |
string systemFileName = GenerateFileName(files.FileName); | |
string blobstorageconnection = _configuration.GetValue<string>("BlobConnectionString"); | |
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection); | |
CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient(); | |
CloudBlobContainer container = blobClient.GetContainerReference(_configuration.GetValue<string>("BlobContainerName")); | |
CloudBlockBlob blockBlob = container.GetBlockBlobReference(systemFileName); | |
blockBlob.Properties.ContentType = "image/jpg"; | |
await using (var data = files.OpenReadStream()) | |
{ | |
await blockBlob.UploadFromStreamAsync(data); | |
} | |
var fileUrl = blockBlob.Uri.AbsoluteUri; | |
return fileUrl; | |
} | |
// Delete the image | |
public async Task Delete(string? FileName) | |
{ | |
FileName = Path.GetFileName(FileName); | |
BlobContainerClient client = new BlobContainerClient(_configuration.GetValue<string>("BlobConnectionString"), _configuration.GetValue<string>("BlobContainerName")); | |
BlobClient file = client.GetBlobClient(FileName); | |
await file.DeleteIfExistsAsync(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment