I'm using following code to upload an image to Azure blob storage. Image is uploaded to the storage but it's broken. Following is my code. The Base64 string is correct and I have confirmed it.
string match_val = "z5FuyuNiJDbbb...................";
System.Drawing.Bitmap img = Custom.ConertBase64ToFile(match_val);
var bytes = Convert.FromBase64String(match_val);
StorageCredentials creden = new StorageCredentials(accountname, accesskey);
CloudStorageAccount acc = new CloudStorageAccount(creden, useHttps: true);
CloudBlobClient client = acc.CreateCloudBlobClient();
CloudBlobContainer cont = client.GetContainerReference(container);
await cont.CreateIfNotExistsAsync();
await cont.SetPermissionsAsync(new BlobContainerPermissions
{
PublicAccess = BlobContainerPublicAccessType.Blob
});
CloudBlockBlob cblob = cont.GetBlockBlobReference(Path.Combine(ProfileSignaturePath, string.Concat(fileName, ".", FileFormat.png)).Replace(@"\","/"));
cblob.Properties.ContentType = "image/png";
using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bytes), true, true))
{
using (MemoryStream memoryStream = new MemoryStream())
{
image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
await cblob.UploadFromStreamAsync(memoryStream);
blobUrl = cblob.Uri.AbsoluteUri;
}
}
string resUrl = blobUrl;
This is what the browser shows
Blob image url is https://mspmatefilestorage.blob.core.windows.net/production/Resources/ProfilePictures/Signatures/signature_16.png
Is there any issues in my image upload code?
May I know what the match_val is? Is it a base64 encoded string of the png file content?
I quickly created a console project and everything worked fine. Here is my sample:
class Program
{
static string storageAccountName = "storagetest789";
static string storageAccountKey = "G36mc********Zup3h9kzj1w==";
static void Main(string[] args)
{
string match_val = null;
using(FileStream fileStream = File.Open(@"D:\User\Pictures\test.png",FileMode.Open))
using(MemoryStream memoryStream = new MemoryStream()){
fileStream.CopyTo(memoryStream);
match_val = Convert.ToBase64String(memoryStream.ToArray());
}
StorageCredentials storageCredentials = new StorageCredentials(storageAccountName,storageAccountKey);
CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(storageCredentials, true);
CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("pub"); // For convenience, it is a public container, so I do not need to set access permission
cloudBlobContainer.CreateIfNotExists();
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("test2.png");
using(MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(match_val)))
using(Image image = Image.FromStream(memoryStream,true,true))
using(CloudBlobStream cloudBlobStream = cloudBlockBlob.OpenWrite()){
image.Save(cloudBlobStream, System.Drawing.Imaging.ImageFormat.Png);
}
Console.WriteLine(cloudBlockBlob.Uri.AbsoluteUri);
Console.ReadLine();
}
}
I did not have a base64 encoded string. So I generated one in the code. The differences were that:
cloudBlockBlob.OpenWrite()
Finally, I got an url: https://storagetest789.blob.core.windows.net/pub/test2.png
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.