11

Here's the scenario: I have two machines on a LAN network. One of them is acting as a web server. When the second, client machine browser (Firefox) makes a request from the server, it sends the following html

<!DOCTYPE HTML>
<html>
<body>
<img src="C:\Users\General\Desktop\map1.jpg" align="middle">
</body>
</html>

The image however is not displayed. I have tried the following variations to the img tag:

<img src="C:/Users/General/Desktop/map1.jpg" align="middle">
<img src="file:///C:/Users/General/Desktop/map1.jpg" align="middle">
<img src="http://localhost//file:/C:/Users/General/Desktop/map1.jpg" align="middle">

Funny thing is if I view the page source and save the html content to a local file and open it in the browser it works. The exact same html code does not work when fetched from the server, but works when opened on the local machine.

Please note that I'm trying to load the image off the client machine because it is impossible to store an image on the server machine in my scenario. (The server is actually an Arduino Mega without an SD card)

PrashanD
  • 2,643
  • 4
  • 28
  • 58
  • So your image is on the local server right? – U r s u s Jan 28 '15 at 14:22
  • The image is on the client machine that makes the http request from the server machine – PrashanD Jan 28 '15 at 14:24
  • 1
    Map the drive to the server and put its mapped name in, so in Windows Explorer on the server, click `Map Network Drive` and map the client's `C:` to the server's `E:` and then put `E:...` in the HTML. – Mark Setchell Jan 28 '15 at 15:28
  • Nice one.. but the server is not a windows machine. It's an Arduino.. :( – PrashanD Jan 28 '15 at 16:07
  • Possible duplicate of [Why can't I do ?](http://stackoverflow.com/questions/4090712/why-cant-i-do-img-src-c-localfile-jpg) – PrashanD May 18 '17 at 05:34
  • 1
    Does this answer your question? [How to show local picture in web page?](https://stackoverflow.com/questions/4908171/how-to-show-local-picture-in-web-page) – Andrey Ozornin Nov 10 '21 at 14:13

5 Answers5

6

In most recent browsers, links to local files ( file:/// ) do not open, for security purposes. In your case, the browser does not display an image that resides on a file on your hard disk. This reason also explains why it works when you save your page locally.

PA.
  • 28,486
  • 9
  • 71
  • 95
1

For starter, you need to add the runat="server" attribute.

If that doesn't suffice, then:

you should change

<img src="http://localhost//file:/C:/Users/General/Desktop/map1.jpg"/>

to something like

<img src="http://localhost/General/Desktop/map1.jpg"/>

or even better to

<img src="~/General/Desktop/map1.jpg"/>

which targets the root of the application (you would need to move your image in that directory)

U r s u s
  • 6,680
  • 12
  • 50
  • 88
1

A html page cannot request images from the client host. It must be stored on the server, or in another remote location.

player
  • 70
  • 11
-2

If you are using Arduino you can:

  1. Use embedded css and images. In result you will got whole page by one browser call

  2. Add additional logic to process browser requests for getting css and jpg files from SD card filesystem of Arduino

-3

it can be solved like this:

.img-class
{
  background-image:url("../../resources/myImage.jpg");
  width: 100%;
  height: 100%;
}

<img  className='img-class'/>
Jacob
  • 1
  • 1
  • 1