本文介绍了在Azure存储中获取文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Azure存储中获取图像文件或Blob的文件类型?我已经研究了一切,但无济于事.任何回应将不胜感激!我想知道,有什么我可以添加以获得的吗?这是我的代码:

Is it possible to get the file type of an image file or blob in Azure Storage? i have researched everything but no to avail. any response will be appreciated! I was wondering, is there anything that i could add in order to get it? here is my code:

ImageSource wew=new BitmapImage(new Uri("http://sweetapp.blob.core.windows.net/cakepictures/"+newfilename+filetypeofthepicture, UriKind.RelativeOrAbsolute)); //need to get the file type of the image
CakeData.Add(new CakeData { Cakename = item.Cakename, ImagePath = wew });

推荐答案

每个Blob都有一个名为Content-Type的属性,可以在上载该Blob时进行设置.您可以利用Storage Client Library来获取Blob属性,以获取其内容类型属性.如果您使用的是.Net Storage Client Library,则可以使用以下代码:

Each blob has a property called Content-Type which can be set when the blob is uploaded. You can make use of Storage Client Library to fetch blob properties to get its content type property. If you are using .Net Storage Client Library, you could use code below:

    var blob = new CloudBlockBlob(new Uri("blob uri"), new StorageCredentials("sweetapp", "account key"));
    blob.FetchAttributes();
    var contentType = blob.Properties.ContentType;

但是,这将要求您在客户端应用程序中包括凭据.如果您不想这样做,另一种选择是使用Shared Access Signature Token并使用它创建StorageCredentials对象的实例.您可以在服务器上的某个位置创建此SAS令牌.

This would however require you to include the credentials in your client app. If you don't want to do that, other alternative would be to use Shared Access Signature Token and use that to create an instance of StorageCredentials object. You could create this SAS token somewhere on the server.

    var credentials = new StorageCredentials(sasToken);
    var blob = new CloudBlockBlob(new Uri("blob uri"), credentials);
    blob.FetchAttributes();
    var contentType = blob.Properties.ContentType;

第三种选择是访问注册表并根据文件扩展名获取mime类型,但我不确定Windows 8应用程序是否可以访问注册表.

3rd alternative would be access the registry and get the mime-type based on file extension but I'm not sure if a Windows 8 app would have access to the registry.

最后的选择是对应用程序中的硬代码进行填充.有一组预定义的mime类型,您可以在应用程序中对其进行硬编码,并根据文件的扩展名来检索内容类型.

Last alternative would be to hard code stuff in your application. There's a predefined set of mime-types which you can hard code in your application and based on the file's extension, you can retrieve the content type.

这篇关于在Azure存储中获取文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:18