0

I would like to store my images in a directory, say

C:/Photos

But to display these images in my application using the img element, I have to use a pathname that is relative to my project directory. For example

<img src="@Url.Content("~/photos/image.jpg")">

Instead of doing that, is there a way I can use an absolute pathname instead, or is it impossible?

arame3333
  • 9,887
  • 26
  • 122
  • 205
  • 1
    `file:///c:/photos/image.jpg` if no server – mplungjan Aug 07 '17 at 07:44
  • Try using: @HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)@Url.Content("~/photos/image.jpg") Source: https://stackoverflow.com/a/13936372/2048391 – jyrkim Aug 07 '17 at 12:32

1 Answers1

1

You can use absolute path names if you really want, of course. They would start with a single backslash on windows ("\").

In a typical production environment (webserver) you can link to your images with the full http address also in an absolute way:

<img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png" />

But beware of the problems this may bring when you switch to a different machine etc.

Happysmithers
  • 733
  • 2
  • 8
  • 13
  • So for the C:/Photos directory it will be "\Photos"? – arame3333 Aug 07 '17 at 07:44
  • I think this should work, yes. You can also try "file:///C:/Photos/image.jpg". But who should be able to view this page, only people using your local computer? – Happysmithers Aug 07 '17 at 07:48
  • I was thinking it would be stored on the C drive of the server. But of course the point you are making is that it will not be. What I want to avoid is having a directory relative to the project folder. – arame3333 Aug 07 '17 at 07:53
  • It is very likely that your server will be a Unix environment, and as such there will not even be a "C drive" but a Unix path. You will however be able to address your image with an absolute path still, in the fashion of the example in the original answer. Still I am doubting absolute paths are really required - what is the reason you want them? -- If the answer has helped you, you can upvote it and mark it as "correct answer". – Happysmithers Aug 07 '17 at 08:01
  • I am using windows. When I log onto the server I see a directory structure similar to my local PC with a C drive and others. In your original answer, is the images folder relative for the project folder? If not where can it be found? – arame3333 Aug 07 '17 at 08:09
  • Post the link to your website please. In my original answer I showed as an example an absolute http path. Where to find it on the drive depends on where the *web server's* root directory is. Most likely it will be the one into which you put your index.html file or such. – Happysmithers Aug 07 '17 at 08:13