I receive the image data as base 64 encoded string and I need to display the contents of the image without downloading it to the server.
My image is not displaying properly when the URL is accessed in a browser
public async static Task<dynamic> UploadImageToStorage(string imageData)
{
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(BlobStorageConnectionString);
try
{
CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(BlobStorageContainer);
if (await container.CreateIfNotExistsAsync())
{
await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
}
string blobID= Guid.NewGuid().ToString();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobID);
blockBlob.Properties.ContentType = "image/jpeg";
await blockBlob.UploadTextAsync(imageData);
return blockBlob.Uri.AbsoluteUri;
//return blobID;
}
it shows like this
I found the solution
I converted the incoming string (base 64 image data) to a byte array and stored in memory stream and uploaded to blob.
try
{
CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(BlobStorageContainer);
if (await container.CreateIfNotExistsAsync())
{
await container.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
}
string blobID= Guid.NewGuid().ToString();
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobID);
blockBlob.Properties.ContentType = "image/jpeg";
byte[] imageBytes = Convert.FromBase64String(imageData);
MemoryStream memStream = new MemoryStream(imageBytes);
await blockBlob.UploadFromStreamAsync(memStream);
return blockBlob.Uri.AbsoluteUri;
}