Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give src to the <img> tag in html from the system drive?

Tags:

html

image

I want to display an image on a web page using HTML <img> tag.

I have stored the image on my local drive.

How to achieve this?

The code:

<img src="D:\Images\TechnipLogo.jpg">

Image is not displayed.

like image 591
Stephen L Avatar asked Feb 12 '14 13:02

Stephen L


People also ask

How do I src an image in HTML?

HTML Images Syntax The <img> tag is empty, it contains attributes only, and does not have a closing tag. The <img> tag has two required attributes: src - Specifies the path to the image. alt - Specifies an alternate text for the image.

How do I display an image in a directory in HTML?

In HTML, the <img> tag is used to add an image from the folder to the HTML document. The image path is specified by the src attribute of <img> tag. To specify the path, firstly write the folder name, then a backslash “/” and lastly, write the image name with its extension.


4 Answers

You must use the correct local path from your PC and the correct Drive letter. Then change the backslashs () to forward slashes (/) and add file:/// before the path, so:

<img src="D:\Images\TechnipLogo.jpg">

becomes:

<img src="file:///D:/Images/TechnipLogo.jpg">

**Also, please note that in Chrome and possibly other browsers, you may not be able to use a local file/image on a site that is not locally hosted. Please reference this post: Cannot open local file - Chrome: Not allowed to load local resource

like image 111
nktsamba Avatar answered Sep 28 '22 00:09

nktsamba


You can look into my answer. I have resolved the issue using C# language Why can't I do <img src="C:/localfile.jpg">?

<asp:Image ID="imgEvid" src="#" runat="server" Height="99px"/>
                        if (File.Exists(filepath))
                        {
                            byte[] imageArray = System.IO.File.ReadAllBytes(filepath);
                            string base64ImageRepresentation = Convert.ToBase64String(imageArray);
                            var val = $"data: image/png; base64,{base64ImageRepresentation}";
                            imgEvid.Attributes.Add("src", val);
                        }

Hope this will help

like image 27
Naveed Khan Avatar answered Sep 28 '22 02:09

Naveed Khan


Your image should be on a relative path not absolute one.

Say your html file is in D:\myhtml\home.html. Copy the Images folder in myhtml. Then change your code to <img src="Images\TechnipLogo.jpg" />.

Hope this helps

like image 35
Kevin Joymungol Avatar answered Sep 28 '22 01:09

Kevin Joymungol


What you are trying to accomplish is a security feature present in all modern browsers.

You could do that only if you run the html file stored locally - it will not work once you deploy it to a web server.

If you really MUST do that, you could build a browser extension - Firefox extensions and IE extensions can access local resources. Chrome is much more restrictive however.

like image 40
korhner Avatar answered Sep 28 '22 02:09

korhner