简体   繁体   中英

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;
        }

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM