Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute file path to relative URL [duplicate]

I've seen lots of tutorials on resolving a relative url to an absolute path, but i want to do the opposite: resolve an system absolute filepath into a relative url.

Is there a nice hack-free way to turn a filepath like c:\my_website_root\images\picture_a.jpg into images/picture_a.jpg

I've had a look at Uri.MakeRelative() but i dont think it will be of use in this case.

Edit: I've implemented it like this, still seems hacky (esp line#2)

var urlPath = new Uri(@"c:\mywebfolder\images\picture1.jpg");
var urlRoot = new Uri(Server.MapPath("~")+"/");
string relative = urlRoot.MakeRelativeUri(urlPath).ToString();
like image 838
maxp Avatar asked Feb 01 '10 10:02

maxp


People also ask

How do you convert an absolute path to a relative path?

The absolutePath function works by beginning at the starting folder and moving up one level for each "../" in the relative path. Then it concatenates the changed starting folder with the relative path to produce the equivalent absolute path.

Is it better to use relative or absolute paths?

Relative links show the path to the file or refer to the file itself. A relative URL is useful within a site to transfer a user from point to point within the same domain. Absolute links are good when you want to send the user to a page that is outside of your server.

What is absolute URL relative URL?

An absolute URL contains all the information necessary to locate a resource. A relative URL locates a resource using an absolute URL as a starting point. In effect, the "complete URL" of the target is specified by concatenating the absolute and relative URLs.


1 Answers

In IIS, setup a virtual directory images and point it to c:\my_website_root\images\.

If your website is already directing to c:\my_website_root\, you don't need to do anything.

like image 144
Oded Avatar answered Oct 10 '22 07:10

Oded