dotnetnukedotnetnuke-moduledotnetnuke-7dotnetnuke-settings

DotNetNuke get URL for image uploaded via the FilePicker


I have a dnn:FilePicker on my settings control for my module. I can get the path for the image from FilePicker.FilePath. That gives me something like:

/Images/Headers/Default.jpg

How on earth do I go from that, to:

/[virtual directory]/portals/0/Images/Header/Default.jpg

This doesn't seem like something that should be hard to do, but I cannot come up with the right combination of words in Google to get an answer.

Right now I'm doing something like:

imgPhoto.ImageUrl = "http://"+PortalAlias.HTTPAlias+"/portals/0/"+DefaultHeaderImage;

Where PortalAlias.HTTPAlias resolves to "localhost/[virtual directory]" and DefaultHeaderImage is a property holding the value saved from FilePicker.FilePath

I've read that using Alias is a bad idea and I'm not at all a fan of using the fully qualified URL instead of the relative URL.

There has got to be a better way.


Solution

  • I use the following code to get the relative path of the image I upload using the dnn:FilePicker:

    using DotNetNuke.Services.FileSystem;
    ...
    
    var image = (FileInfo)FileManager.Instance.GetFile(FilePicker.FileID);
    if (image != null)
    {
        imgPhoto.ImageUrl = FileManager.Instance.GetUrl(image);
    }
    

    If you need to full url, I use the following code:

    string baseUrl = (Request.IsSecureConnection ? "https://" : "http://") + base.PortalAlias.HTTPAlias;
    var image = (FileInfo)FileManager.Instance.GetFile(FilePicker.FileID);
    if (image != null)
    {
        imgPhoto.ImageUrl = baseUrl + FileManager.Instance.GetUrl(image);
    }