Try our conversational search powered by Generative AI!

Is there a universal, storage-agnostic way of reading files from MediaData content?

Vote:
 

We need to read media files. So, a text file is stored "for this page" or "for this block" and we need to read the contents of it from code.

I was doing this quite fine, but then I found out that my method (below) required the provider to be the local file system.

System.IO.File.ReadAllText(media.BinaryDataContainer.AbsolutePath);

As soon as we deployed to Azure, this fell apart.

I have to assume there is a universal, storage-agnostic way of reading files from MediaData objects, but Google is polluted with code from several versions back. What's the latest, accepted way of doing this that will work no matter where a site is hosted?

#188254
Feb 15, 2018 18:34
Vote:
 

So, this works, but I'm still curious if this is storage-agnostic. Will this work for every possible storage provider?

var blob = media.BinaryData;
using (var stream = blob.OpenRead())
{
    var reader = new StreamReader(stream);
    return reader.ReadToEnd();
}
#188255
Feb 15, 2018 19:36
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.