0

I have a project where I need to display images and videos. I am saving both image and videos URL inside a table called Images and while retrieving I am using image handler for images to resize the image on the server level. Right now my code to display images and video is just this line

<td>
                                <img src="~/ImageHandler.ashx?file=@Html.DisplayFor(modelItem => item.url)" />
                            </td>

I need something like below to identify the file extension before displaying. if the extension is png or jpeg go to this line else this line. How can I do like below or any other better option?

                        @foreach (var item in Model)
                        {
                            <tr>
                                if (extension == .png || extension == .jpeg )
                                {
                                <td>
                                    <img src="~/ImageHandler.ashx?file=@Html.DisplayFor(modelItem => item.url)" />
                                </td>
                                }
                                else
                                {
                                <td>
                                    <video width="240" height="240" autoplay>
                                        <source src="@Html.DisplayFor(modelItem => item.url)" type="video/mp4">
                                    </video>
                                </td>
                                }
                                <td>
                                    @Html.DisplayFor(modelItem => item.details)
                                </td>
                            </tr>
                        }
Nav
  • 71
  • 13
  • You could use something like [this answer](https://stackoverflow.com/a/11945256/2557128) with the comment for HEAD only to probe for the mime-type of the URL. – NetMage Jan 30 '19 at 22:44

2 Answers2

2

If the model only contains the url/path to the file you could split the string with the delimiter as "." and get the last element in the resulting array of strings.
Eg:

@foreach(var item in Model)
{
    var extn = item.url.Split(".").Last();
    if (extn == ".png" || extn == ".jpeg" )
    {
        @*Do image display*@
    }
    else
    {
       @*Do video display*@
    }
}

Not sure if this is optimal, but i cant think of another way to do it.

Jason Brown
  • 91
  • 1
  • 7
  • 2
    You are using `extn` and `extension` in your code in different places. The `System.IO.Path.GetExtension` method is the recommended way to get the extension for a file path. – NetMage Jan 30 '19 at 22:42
  • @Jason Brown Thanks!! it worked perfectly. But to get the extension as NetMage suggested I used System.IO.Path.GetExtension. – Nav Jan 31 '19 at 14:10
0

You can use HttpClient to check for the Content-Type of the URL and then decide how to handle it:

public static class URLExt {
    public static string GetMimeType(this string url) {
        using (var client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true })) {
            var r = client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false).GetAwaiter().GetResult();
            return r.IsSuccessStatusCode ? r.Content.Headers.ContentType.MediaType : null;
        }
    }
}
NetMage
  • 26,163
  • 3
  • 34
  • 55