2017-02-21 138 views
0

我試圖從圖像Azure的Blob存儲轉換爲Base64:將圖像從azure blob存儲轉換爲Base64?

private static string FromAzureToBase64(string azureUri) 
{ 
    Uri blobUri = new Uri(azureUri); 
    CloudBlockBlob blob = new CloudBlockBlob(blobUri, StorageAccount.Credentials); 

    byte[] arr = new byte[blob.Properties.Length]; 
    blob.DownloadToByteArray(arr, 0); 
    var azureBase64 = Convert.ToBase64String(arr); 
    return azureBase64; 
} 

arr參數的問題是,我應該定義它的長度,但blob.Properties.Length值爲-1,但是在圖像上存在Azure的,但幾乎所有的屬性null或不確定:

enter image description here

回答

6

你可以做的是獲取BLOB的屬性,然後一滴的length屬性將被填充。所以你的代碼是:

private static string FromAzureToBase64(string azureUri) 
{ 
    Uri blobUri = new Uri(azureUri); 
    CloudBlockBlob blob = new CloudBlockBlob(blobUri, StorageAccount.Credentials); 
    blob.FetchAttributes();//Fetch blob's properties 
    byte[] arr = new byte[blob.Properties.Length]; 
    blob.DownloadToByteArray(arr, 0); 
    var azureBase64 = Convert.ToBase64String(arr); 
    return azureBase64; 
}