tags:

views:

4895

answers:

5

Basically I have some code to check a specific directory to see if an image is there and if so I want to assign a URL to the image to an ImageControl.

    if (System.IO.Directory.Exists(photosLocation))
    {
        string[] files = System.IO.Directory.GetFiles(photosLocation, "*.jpg");
        if (files.Length > 0)
        {
            // TODO: return the url of the first file found;
        }
    }
A: 

So far as I know there's no single function which does this (maybe you were looking for the inverse of MapPath?). I'd love to know if such a function exists. Until then, I would just take the filename(s) returned by GetFiles, remove the path, and prepend the URL root. This can be done generically.

James D
A: 

I think this should work. It might be off on the slashes. Not sure if they are needed or not.

string url = Request.ApplicationPath + "/" + photosLocation + "/" + files[0];

Robin Robinson
+4  A: 

Afaik, there's no method to do what you want directly. I'd store the photosLocation as a path relative to the application; f.ex. "~/Images/". This way, you could use MapPath to get the physical location, and ResolveUrl to get the url (with a bit of help from System.IO.Path):

string photosLocationPath = HttpContext.Current.Server.MapPath(photosLocation);

if (Directory.Exists(photosLocationPath))
{
    string[] files = Directory.GetFiles(photosLocationPath, "*.jpg");
    if (files.Length > 0)
    {
        string filenameRelative = photosLocation + 
                                  Path.GetFilename(files[0])     

        return HttpContext.Current.Request.ResolveUrl(filenameRelative);
    }
}
Fredrik Kalseth
I can't find the ResolveUrl member of HttpRequest in the documentation for any of the .Net versions. Are you using ASP.Net MVC?
Jared
@Fredrik As Jared has pointed out the HttpRequest object doesn't have this method. It can be found in the Page object or a web control object. Could you edit your answer to reflect this?
Andy Rose
+2  A: 

Maybe this is not the best way, but it works.

// Here is your path
String p = photosLocation + "whatever.jpg";

// Here is the page address
String pa = Page.Request.Url.AbsoluteUri;

// Take the page name    
String pn = Page.Request.Url.LocalPath;

// Here is the server address    
String sa = pa.Replace(pn, "");

// Take the physical location of the page    
String pl = Page.Request.PhysicalPath;

// Replace the backslash with slash in your path    
pl = pl.Replace("\\", "/");    
p = p.Replace("\\", "/");

// Root path     
String rp = pl.Replace(pn, "");

// Take out same path    
String final = p.Replace(rp, "");

// So your picture's address is    
String path = sa + final;

Edit: Ok, somebody marked as not helpful. Some explanation: take the physical path of the current page, split it into two parts: server and directory (like c:\inetpub\whatever.com\whatever) and page name (like /Whatever.aspx). The image's physical path should contain the server's path, so "substract" them, leaving only the image's path relative to the server's (like: \design\picture.jpg). Replace the backslashes with slashes and append it to the server's url.

Biri
A: 

I've accepted Fredriks answer as it appears to solve the problem with the least amount of effort however the Request object doesn't appear to conatin the ResolveUrl method. This can be accessed through the Page object or an Image control object:

myImage.ImageUrl = Page.ResolveUrl(photoURL);
myImage.ImageUrl = myImage.ResolveUrl(photoURL);

An alternative, if you are using a static class as I am, is to use the VirtualPathUtility:

myImage.ImageUrl = VirtualPathUtility.ToAbsolute(photoURL);
Andy Rose