简体   繁体   English

在Azure存储中获取文件类型

[英]Getting the File type in Azure Storage

Is it possible to get the file type of an image file or blob in Azure Storage? 是否可以在Azure存储中获取图像文件或Blob的文件类型? 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 });

Each blob has a property called Content-Type which can be set when the blob is uploaded. 每个Blob都有一个称为Content-Type的属性,可以在上传Blob时进行设置。 You can make use of Storage Client Library to fetch blob properties to get its content type property. 您可以利用Storage Client Library来获取Blob属性,以获取其内容类型属性。 If you are using .Net Storage Client Library, you could use code below: 如果使用的是.Net Storage Client Library,则可以使用以下代码:

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

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. 如果您不想这样做,则另一种选择是使用Shared Access Signature Token并使用它来创建StorageCredentials对象的实例。 You could create this SAS token somewhere on the server. 您可以在服务器上的某个位置创建此SAS令牌。

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

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类型,但我不确定Windows 8应用程序是否可以访问注册表。

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. 有一组预定义的mime类型,您可以在应用程序中对其进行硬编码,并根据文件的扩展名来检索内容类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM