web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / heralddyncrm / Upload and Download Files i...

Upload and Download Files in Azure Blob using C#

Herald Jeramiah Profile Picture Herald Jeramiah 35

Hi Folks, Today i will explain how to Create Azure Blob Storage and Upload, Download files from Azure Blob Storage using C#.

Step 1: Create Azure Blob Storage

Open Portal.Azure.com.

Navigate to Storage Account.

1

Add new Storage Account by Clicking Add Button.

2

Navigate To Blob Service Section as shown below

3

Next Create Container for the Blob to Add files into the Container using Upload Button as shown below.

4.PNG

5

Navigate to Storage Account(Which you created) -> AccessKey , Copy Connectionstring which is used to connect Blob from C# Code.

6

Step 2: Upload File into Azure Blob using C#

Add the Required reference using Nuget

  1. using Microsoft.Azure;
  2. using Microsoft.WindowsAzure.Storage;
  3. using Microsoft.WindowsAzure.Storage.Blob;

Add the Azure Storage Account Connection String in App.config which i explained in previous Screenshot. Copy the below code and Reference the file which is to be uploaded . The code will work like charm.

 

string storageConnection = CloudConfigurationManager.GetSetting("BlobStorageConnectionString"); CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnection);

//create a block blob CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();

//create a container CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("appcontainer");

//create a container if it is not already exists

if (await cloudBlobContainer.CreateIfNotExistsAsync()) {

await cloudBlobContainer.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

}

string imageName = "Test-" + Path.GetExtension(imageToUpload.FileName);

//get Blob reference

CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(imageName); cloudBlockBlob.Properties.ContentType = imageToUpload.ContentType;

await cloudBlockBlob.UploadFromStreamAsync(imageToUpload.InputStream); 

Step 3: Download File from Azure Blob using C#

var containerName = "testcontainerherbi";

string storageConnection = CloudConfigurationManager.GetSetting("BlobStorageConnectionString"); CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnection); CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();



CloudBlobContainer cloudBlobContainer = blobClient.GetContainerReference(containerName); CloudBlockBlob blockBlob = cloudBlobContainer.GetBlockBlobReference("uploadedfilename.ext");



MemoryStream memStream = new MemoryStream();

blockBlob.DownloadToStream(memStream);

HttpContext.Current.Response.ContentType = blockBlob.Properties.ContentType.ToString(); HttpContext.Current.Response.AddHeader("Content-Disposition", "Attachment; filename=" + blockBlob.ToString());

HttpContext.Current.Response.AddHeader("Content-Length", blockBlob.Properties.Length.ToString()); HttpContext.Current.Response.BinaryWrite(memStream.ToArray()); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Close();

Hope It's Helpful.  Eat-> Code->Sleep->Repeat.

Comments

*This post is locked for comments

  • Jayakumarr Profile Picture Jayakumarr 10
    Posted at
    Hi Hello can you update your article ? how to upload files in storage container in side folder?
  • Jayakumarr Profile Picture Jayakumarr 10
    Posted at
    Hi How to receive StorageUri after this line invoked await cloudBlockBlob.UploadFromStreamAsync(imageToUpload.InputStream)
  • Jayakumarr Profile Picture Jayakumarr 10
    Posted at
    Hi I am using your code . when the following 2 lines are executed following error encounter Code : =========== MemoryStream memStream = new MemoryStream(); blockBlob.DownloadToStream(memStream); Error: =========== The remote server returned an error: (404) Not Found.
  • Jayakumarr Profile Picture Jayakumarr 10
    Posted at
    Hi Your code error shows like this The remote server returned an error: (404) Not Found. can you resolve it?