Why can't I do <img src="C:/localfile.jpg">?

HtmlSrc

Html Problem Overview


It works if the html file is local (on my C drive), but not if the html file is on a server and the image file is local. Why is that?

Any possible workarounds?

Html Solutions


Solution 1 - Html

It would be a security vulnerability if the client could request local file system files and then use JavaScript to figure out what's in them.

The only way around this is to build an extension in a browser. Firefox extensions and IE extensions can access local resources. Chrome is much more restrictive.

Solution 2 - Html

shouldn't you use "file://C:/localfile.jpg" instead of "C:/localfile.jpg"?

Solution 3 - Html

Browsers aren't allowed to access the local file system unless you're accessing a local html page. You have to upload the image somewhere. If it's in the same directory as the html file, then you can use <img src="localfile.jpg"/>

Solution 4 - Html

C: is not a recognized URI scheme. Try file://c|/... instead.

Solution 5 - Html

Honestly the easiest way was to add file hosting to the server.

  • Open IIS

  • Add a Virtual Directory under Default Web Site

    • virtual path will be what you want to browse to in the browser. So if you choose "serverName/images you will be able to browse to it by going to http://serverName/images
    • Then add the physical path on the C: drive
  • Add the appropriate permissions to the folder on the C: drive for "NETWORK SERVICE" and "IIS AppPool\DefaultAppPool"

  • Refresh Default Web Site

  • And you're done. You can now browse to any image in that folder by navigating to http://yourServerName/whateverYourFolderNameIs/yourImage.jpg and use that url in your img src

Hope this helps someone

Solution 6 - Html

we can use javascript's FileReader() and it's readAsDataURL(fileContent) function to show local drive/folder file. Bind change event to image then call javascript's showpreview function. Try this -

<!doctype html>
<html>

<head>
    <meta charset='utf-8'>
    <meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no;'>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
    <title></title>

	<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
	
	<script type="text/javascript">
	function showpreview(e) {
		var reader = new FileReader();
        reader.onload = function (e) {
            $("#previewImage").attr("src", e.target.result);
        }
		//Imagepath.files[0] is blob type
        reader.readAsDataURL(e.files[0]);
    }
	</script>
</head>
<body >
	<div>
	<input type="file" name="fileupload" value="fileupload" id="fileupload" onchange='showpreview(this)'>
	</div>
	<div>
		&nbsp;
	</div>
	<div>
    <img width="50%" id="previewImage">
	</div>
</body>
</html>

Solution 7 - Html

IE 9 : If you want that the user takes a look at image before he posts it to the server : The user should ADD the website to "trusted Website list".

Solution 8 - Html

Newtang's observation about the security rules aside, how are you going to know that anyone who views your page will have the correct images at c:\localfile.jpg? You can't. Even if you think you can, you can't. It presupposes a windows environment, for one thing.

Solution 9 - Html

if you use Google chrome browser you can use like this

<img src="E://bulbpro/pic_bulboff.gif"   width="150px" height="200px">

But if you use Mozila Firefox the you need to add "file " ex.

<img src="file:E://bulbpro/pic_bulboff.gif"   width="150px" height="200px">

Solution 10 - Html

I see two possibilities for what you are trying to do:

  1. You want your webpage, running on a server, to find the file on the computer that you originally designed it?

  2. You want it to fetch it from the pc that is viewing at the page?

Option 1 just doesn't make sense :)

Option 2 would be a security hole, the browser prohibits a web page (served from the web) from loading content on the viewer's machine.

Kyle Hudson told you what you need to do, but that is so basic that I find it hard to believe this is all you want to do.

Solution 11 - Html

If you're deploying a local website just for yourself or certain clients, you can get around this by running mklink /D MyImages "C:/MyImages" in the website root directory as an admin in cmd. Then in the html, do <img src="MyImages/whatever.jpg"> and the symbolic link established by mklink will connect the relative src link with the link on your C drive. It solved this issue for me, so it may help others who come to this question.

(Obviously this won't work for public websites since you can't run cmd commands on people's computers easily)

Solution 12 - Html

I have tried a lot of techniques and finally found one in C# side and JS Side. You cannot give a physical path to src attribute but you can give the base64 string as a src to Img tag. Lets look into the below C# code example.

<asp:Image ID="imgEvid" src="#" runat="server" Height="99px"/>

C# code

 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

Solution 13 - Html

starts with file:/// and ends with filename should work:

<img src="file:///C:/Users/91860/Desktop/snow.jpg" alt="Snow" style="width:100%;">

Solution 14 - Html

You need to upload the image aswell, then link to the image on the server.

Solution 15 - Html

what about having the image be something selected by the user? Use a input:file tag and then after they select the image, show it on the clientside webpage? That is doable for most things. Right now i am trying to get it working for IE, but as with all microsoft products, it is a cluster fork().

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeterVView Question on Stackoverflow
Solution 1 - HtmlBjornView Answer on Stackoverflow
Solution 2 - HtmlAndreDuraoView Answer on Stackoverflow
Solution 3 - HtmlNewtangView Answer on Stackoverflow
Solution 4 - HtmlIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 5 - HtmlforemaroView Answer on Stackoverflow
Solution 6 - HtmlRavindra VairagiView Answer on Stackoverflow
Solution 7 - HtmlUnited4PeaceView Answer on Stackoverflow
Solution 8 - HtmlJack KellyView Answer on Stackoverflow
Solution 9 - HtmlSachindra N. PandeyView Answer on Stackoverflow
Solution 10 - HtmlJuan MendesView Answer on Stackoverflow
Solution 11 - Htmltakanuva15View Answer on Stackoverflow
Solution 12 - HtmlNaveed KhanView Answer on Stackoverflow
Solution 13 - HtmlSteveView Answer on Stackoverflow
Solution 14 - HtmlKyle HudsonView Answer on Stackoverflow
Solution 15 - HtmlFallenreaperView Answer on Stackoverflow