简体   繁体   中英

Get URL path for image from episerver blob file

I'm creating a blob file using the following method in my code

Adding to Blob

 Thumbnail = MediaToBlob(thumbMediaUrl, accessToken, ".jpg", blobFactory, qbankMedia, "text/html");
                                    blobCache.Add(videoMediaUrl, qbankMedia.BinaryData);

MediaToBlob Method

public Blob MediaToBlob(string mediaUrl, string accessToken, string extension, IBlobFactory blobFactory, IQBankEpiMedia qbankMedia, string mimeType, int maxLength = int.MaxValue)
        {
            var mediaStream = GetMediaStream(mediaUrl, accessToken, mimeType, maxLength);

            Blob blob = null;

            if (mediaStream != null)
            {
                blob = blobFactory.CreateBlob(qbankMedia.BinaryDataContainer, extension);

                using (var blobStream = blob.OpenWrite())
                {
                    byte[] buffer = new byte[64 * 1024];
                    int read;
                    while ((read = mediaStream.Read(buffer, 0, buffer.Length)) > 0)
                        blobStream.Write(buffer, 0, read);
                    mediaStream.Flush();
                    mediaStream.Close();
                }
            }

            return blob;
        }

I Need to get a relative path to this Thumbnail object so that I can use it to render the image in site. I can use Thumbnail.ID.AbsoluteUri to get this:

epi.fx.blob://default/f791355aa6334c92b5f370c5cfafd971/adc882777a04431baae83a2e05f3f02d.jpg

instead of that, i need something like this:

http://localhost:8000/episerver/f791355aa6334c92b5f370c5cfafd971/adc882777a04431baae83a2e05f3f02d.jpg

Can anyone guide me to have something that I can use as normal Url for images?

You can access thumbnail properties by appending the property name to the URL:

private readonly IUrlResolver _urlResolver;

var thumbnailUrl = _urlResolver.GetUrl(imageContentReference) + "/thumbnail";

This works for any thumbnail property your content type might have. 'Thumbnail' is the default propety available on ImageData.

When you load an image or a media file into Episerver, it will be treated as content, it will have a content reference and consequently, you will also be able to get assets using it. So when saving a file to the blob you should then use that to save the media content. You can then get the url like you do to any content in episerver.

I would recomend checking these two blog post by Jon

How to upload an image into episerver cms via code

How to get the url for an image in episerver

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