0

Is it possible to create an HTML image, if I have only a path to a local file? I tried to use a filereader, but the mere path does not work. how can I solve the issue?

JS

var reader = new FileReader();
reader.onload = {
     $('#myImg').attr('src', e.target.result);
};
   reader.readAsDataURL("file:///C:/Users/me/AppData/Local/Temp/msohtmlclip1/01/clip_image002.jpg  ");
1
  • 2
    You can't read files from a file:// protocol for security reasons. You can turn off the security in chrome at least in the browser options if this is for some internal testing etc. Commented Apr 12, 2014 at 21:37

3 Answers 3

2

This is a simple tool I have made for reading files in JavaScript:

Fiddle

The JavaScript code is:

var reader = new FileReader();
reader.onerror = function(ev) {
    $('#output').html('=== Error reading file ===');
}
reader.onload = function(ev) {
    $('#output').html(ev.target.result);
};
reader.readAsDataURL(e.target.files[0]);

When you select an image file it will present you with a base64 dataURI of the image.

I recommend not trying to select a file that's not an image, I don't know what'll happen.

Sign up to request clarification or add additional context in comments.

Comments

0

something like this?

var x=document.createElement("img");
x.src="C:\data\images\test.jpg";
x.style.height="50px";
document.getElementById('whereimgoing').appendChild(x);

Also I should add that if this is on a website then it will depend highly on browser security

Comments

-1
var reader = new FileReader();
reader.onload = function(e) {
 $('#myImg').attr('src', reader.result);
};
reader.readAsDataURL("file:///C:/Your/path/msohtmlclip1/01/clip_image002.jpg");

Should be fine, if access to local files is granted (check your browser settings or try if it works when deployed on a server (either localhost or www.yourserver.com).. Local files can always cause some troubles as browser behave differently. Also try to not use the temp folder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.