0

I have a piece of code that is like

string.Format("<a href='{0}' class='hidden'>{0}</a>", Path.Combine(filePathPrefix , string.Join("-", new string[] { fOrgName, fCatName, f.filename })))

and meant to return an a tag that a user can click on to get a file. The only problem is that string I'm building for the href does not work as a link. It is something like

c:\users\me\documents\visual studio 2013\Projects\myproj\myproj\Assets\someorg-somecat-somepic.png

which doesn't work as a clickable link, brings to an about:blank page if you click on it. However, if I paste the link into my browser and make the request then it changes to

file:///C:/users/me/documents/visual%20studio%202013/Projects/mypoj/myproj/Assets/someorg-somecat-somepic.png

and brings up the asset properly. That means I need some way getting the link in that form while its on the page. Is this possible? If so, what C# class(es) and method(s) do I need?

user4905335
  • 371
  • 2
  • 13
  • 1
    Have you tried this: http://stackoverflow.com/questions/1546419/convert-file-path-to-a-file-uri It looks like this may work and if so, you would need System.Uri – Cory May 18 '15 at 15:36
  • 3
    Realize that the url **will only work** for you. – Erik Philips May 18 '15 at 15:46
  • That looks exactly like what I was talking about in my question! The link still isn't clickable, though. I guess there's something else still missing. – user4905335 May 18 '15 at 15:49
  • @ErikPhilips makes a very important point. This is why you should only ever store relative paths in your database, i.e. `Assets\someorg-somecat-somepic.png`. Then, you can use that in combo with something like `Server.MapPath` to get the full file path if you need it or something like `Url.Content` to get a URL version. – Chris Pratt May 18 '15 at 16:00
  • I'll also mention that this will work for IIS Express but not IIS because IIS will not have access to any users folder (i.e. `c:\users\....`). – Erik Philips May 18 '15 at 16:15

1 Answers1

-1

Try changing to this:

string.Format("<a href=\"{0}\" class=\"hidden\">{0}</a>", ...);
Cory
  • 783
  • 5
  • 12
  • Ok, here we go. He is trying to map a path, the problem is that this path is not going to be available when it's online, because it's pointing to the resource on his machine. So the problem is not on the string.Format, but how he is referring to the resource. – brduca May 18 '15 at 17:43
  • @Brduca That is correct, but the issue I was trying to address was different. Here is what I was aiming to fix "The link still isn't clickable, though. I guess there's something else still missing". – Cory May 18 '15 at 18:06
  • You are trying to solve the problem by changing '{0}' to "{0}". Please take a look at this: http://stackoverflow.com/questions/2373074/single-vs-double-quotes-vs – brduca May 18 '15 at 18:20
  • @Brduca Ah yes, you got me ;-). I thought there could have been an issue with the single quotes. Thank for providing that link, now I know that there is essentially no difference. – Cory May 18 '15 at 18:38