上传 blockblob 并设置 contenttype [英] Uploading blockblob and setting contenttype

查看:23
本文介绍了上传 blockblob 并设置 contenttype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 中的 Microsoft.WindowsAzure.Storage.* 库.

I'm using Microsoft.WindowsAzure.Storage.* library from C#.

这是我将内容上传到存储的方式:

This is how I'm uploading things to storage:

// Store in storage
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("...connection string...");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("pictures");

// Create container if it doesnt exist
container.CreateIfNotExists();

// Make available to everyone
container.SetPermissions(new BlobContainerPermissions
{
    PublicAccess = BlobContainerPublicAccessType.Blob
});

// Save image
CloudBlockBlob blockBlob = container.GetBlockBlobReference("blah.jpg");
blockBlob.UploadFromByteArray(byteArrayThumbnail, 0, byteArrayThumbnail.Length);
blockBlob.Properties.ContentType = "image/jpg";  // *** NOT WORKING ***

我上传到存储的所有内容都以内容类型application/octet-stream"保存,即使我使用值为image/jpg"的 setter(请参阅代码中的最后一行).

All the things I upload to the storage are being saved with content type "application/octet-stream", even though I'm using the setter with value "image/jpg" (see the last line in my code).

那么问题 1:为什么 ContentType 设置器不起作用?

So question #1: Why isn't working the ContentType setter?

和问题#2:如果我手动将内容类型更改为image/jpg",使用 Windows Azure 管理门户,然后将文件的绝对 URI 复制到浏览器的地址字段,然后按 Enter,则 jpg 文件被下载而不是显示.不应该显示这种 mime 类型而不是下载吗?我该如何更改?

And question #2: If I manually change the content type to "image/jpg", using Windows Azure management portal, and then copy the absolute URI of the file to the browser's address field, and press enter, the jpg file is downloaded instead of displayed. Isn't this mime type supposed to be displayed instead of downloaded? How do I change this?

推荐答案

实际上您不必调用 SetProperties 方法.为了在上传 blob 时设置内容类型,只需在调用上传方法之前设置 ContentType 属性.所以你的代码应该是:

Actually you don't have to call SetProperties method. In order to set content type while uploading the blob, just set the ContentType property before calling the upload method. So your code should be:

// Save image
CloudBlockBlob blockBlob = container.GetBlockBlobReference("blah.jpg");
blockBlob.Properties.ContentType = "image/jpg";
blockBlob.UploadFromByteArray(byteArrayThumbnail, 0, byteArrayThumbnail.Length);

这应该可以解决问题.

这篇关于上传 blockblob 并设置 contenttype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆