none
Save Base64 string as Image in Blob Storage

    Question

  • Hi,

    I need to save base 64 string as an image in blob storage. Then use the URl of the blob in the Image src field.

    Can anyone suggest me the possibilities of this.

    Regards,

    Sammani

    Wednesday, April 6, 2016 8:25 AM

Answers

  • Found the solution

      public static string UploadImage(string imageData, ImageType imageType, int cardId)
            {
                string filename = GetFileName(imageType, cardId);
                CloudBlobContainer container = GetContainer("userimages");
                byte[] imageBytes = Convert.FromBase64String(imageData);
                CloudBlockBlob blob = container.GetBlockBlobReference(filename);
                blob.Properties.ContentType = "Jpeg";
                blob.UploadFromByteArray(imageBytes, 0, imageBytes.Length);
                return blob.StorageUri.PrimaryUri.AbsoluteUri;
            }

    Thursday, April 7, 2016 10:52 AM

All replies

  • Hi Sammani,

    In this link explain how upload to blob storage via SDK .Net, you can do the same via RESTAPi. You should convert the string to stream.

    https://azure.microsoft.com/es-es/documentation/articles/storage-dotnet-how-to-use-blobs/

    After that the only thing you need to do is put in src image the URL geneate un BlobStorage.

    Regards


    Wednesday, April 6, 2016 8:37 AM
  • Just a slight adjustment to the URL that Robert posted.

    Try this non-localized one: https://azure.microsoft.com/documentation/articles/storage-dotnet-how-to-use-blobs/

    It's easy to upload your string to blob storage. Just call one of the Upload* methods on the CloudBlockBlob class (assuming your blob is a block blob). UploadText() may be sufficient for your needs.

    Wednesday, April 6, 2016 7:42 PM
  • Hi,

    Thank for the reply.

    But what I need is save base64 string as a image file not as string. Can you please help me on it.

    Regards,

    Sammani

    Thursday, April 7, 2016 4:35 AM
  • For binary data, you can use either CloudBlockBlob.UploadFromStream Method (Microsoft.WindowsAzure.Storage.Blob) or CloudBlockBlob.UploadFromByteArrayAsync Method (Microsoft.WindowsAzure.Storage.Blob).

    See https://github.com/Azure-Samples/storage-blob-dotnet-getting-started/blob/master/BlobStorage/Program.cs for some examples.

    Thursday, April 7, 2016 4:54 AM
  • Found the solution

      public static string UploadImage(string imageData, ImageType imageType, int cardId)
            {
                string filename = GetFileName(imageType, cardId);
                CloudBlobContainer container = GetContainer("userimages");
                byte[] imageBytes = Convert.FromBase64String(imageData);
                CloudBlockBlob blob = container.GetBlockBlobReference(filename);
                blob.Properties.ContentType = "Jpeg";
                blob.UploadFromByteArray(imageBytes, 0, imageBytes.Length);
                return blob.StorageUri.PrimaryUri.AbsoluteUri;
            }

    Thursday, April 7, 2016 10:52 AM
  • Thanks very much
    Wednesday, July 26, 2017 4:08 AM