'How to Uploading Base64 / byte[] Image to Azure Blob with ASP.NET Core 6 MVC using C#?

Here is my code IFormFile.

public static async Task<string> UploadFileToBlobStorage(IFormFile file)
        {
            string container = "abccontainer";
            string storageAccount_connectionString = "DefaultEndpointsProtocol=https;AccountName=abc/aXQUORlrTMNkxaUFPDDlLEW/mwe5Fihy+zl4p+ysF6Y+9JiMud8khH+AStEDb6DA==;EndpointSuffix=core.windows.net";
            BlobContainerClient blobContainerClient = new(storageAccount_connectionString, container);

            BlobClient blob = blobContainerClient.GetBlobClient(Path.GetFileName(file.FileName));

            var mimeTypes = GetFileContentType(Path.GetFileName(file.FileName));
            var header = new BlobHttpHeaders
            {
                ContentType = mimeTypes
            };

            await blob.UploadAsync(file.OpenReadStream(), header);

            return blob.Uri.AbsoluteUri;
        }

But, I want to upload bytes[] to blob storage. Foe that, I will pass only byte[] array, and it should upload.

How can I do ?

public static async Task<string> UploadFileToBlobStorage(byte[] array)
        {
            return blob.Uri.AbsoluteUri;
        }


Solution 1:[1]

UploadAsync has an overload that takes a BinaryData object.

You should be able to rewrite your code like this:

BlobClient blob = blobContainerClient.GetBlobClient("somename.txt"); // update as necessary

var header = new BlobHttpHeaders
{
    ContentType = "text/plain" // update as necessary
};


byte[] sourceData = getSourceData();
BinaryData uploadData = new BinaryData(sourceData);
await blob.UploadAsync(uploadData, header);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pankaj