【发布时间】:2020-01-09 11:46:18
【问题描述】:
我正在使用以下代码将图像上传到 Azure blob 存储。图像已上传到存储,但已损坏。以下是我的代码。 Base64字符串是正确的,我已经确认了。
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;
这是浏览器显示的内容
我的图片上传代码有问题吗?
【问题讨论】:
-
您是否尝试过将内存流保存到文件中,只是为了检查数据在此之前是否正常?
-
另外,上传前尝试将内存流的位置重置为0。
-
您能否检查一下我的回答是否有帮助?谢谢!
标签: c# azure asp.net-core asp.net-web-api azure-blob-storage