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

查看:521
本文介绍了上载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 ***

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

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:如果我使用Windows Azure管理门户手动将内容类型更改为"image/jpg",然后将文件的绝对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时设置内容类型,只需在调用upload方法之前设置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天全站免登陆