Bas*_*maa 3 cloud asp.net-mvc azure
我使用下面的代码将MemoryStream从位图图像上传到我的Microsoft Azure存储帐户:
MemoryStream memoryStream = new MemoryStream();
img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
blob.Properties.ContentType = model.File.ContentType;
blob.UploadFromStream(memoryStream);
Run Code Online (Sandbox Code Playgroud)
使用上面代码发生的是它将空图像上传到Azure存储:(!(我找到了名称,但文件大小为零!)!
if (model.File != null && model.File.ContentLength > 0)
{
Bitmap original = null;
var name = "newimagefile";
var errorField = string.Empty;
errorField = "File";
name = Path.GetFileNameWithoutExtension(model.File.FileName);
original = Bitmap.FromStream(model.File.InputStream) as Bitmap;
if (original != null)
{
var img = CreateImage(original, model.X, model.Y, model.Width, model.Height);
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("company"); // must always be lowercase
container.CreateIfNotExists();
container.SetPermissions(
new BlobContainerPermissions
{
PublicAccess =
BlobContainerPublicAccessType.Blob
});
CloudBlockBlob blob = container.GetBlockBlobReference(imgName + ".png");
MemoryStream memoryStream = new MemoryStream();
img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
blob.Properties.ContentType = model.File.ContentType;
blob.UploadFromStream(memoryStream);
Run Code Online (Sandbox Code Playgroud)
}
请帮忙!
我认为您需要在上传之前将位置设置回流的开头
img.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
blob.Properties.ContentType = model.File.ContentType;
memoryStream.Position = 0;
blob.UploadFromStream(memoryStream);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
892 次 |
| 最近记录: |