Skip to content

Instantly share code, notes, and snippets.

@Dark-Rex01
Created February 2, 2023 04:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dark-Rex01/60e459e395a1f2be67ca156a9fa1063b to your computer and use it in GitHub Desktop.
Save Dark-Rex01/60e459e395a1f2be67ca156a9fa1063b to your computer and use it in GitHub Desktop.
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