简体   繁体   中英

Getting the File type in Azure Storage

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 });

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;

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;

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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