HTML:
Let’s make a simple HTML markup. All I need is an <input> @type=file and an <img> with no @src defined for image preview. Also, I bind an onchange event for the <input> @type=file which calls the PreviewImg() function.
<input id="myFile" type="file" onchange="PreviewImg(this)"/>
<img id="image" src="" style="height:50px;width:50px;" alt="Image Preview" />
<img id="image" src="" style="height:50px;width:50px;" alt="Image Preview" />
JavaScript for Internet Explorer:
I first thought there should be an easy way to retrieve the physical location of image file on user’s hard drive. Once the value is retrieved, set the source of the empty image element to it. As I googled for a solution, here is what I saw about 90% of the time.
function PreviewImg(elem) {
var myImage = document.getElementById('image');
myImage.src = elem.value;
}
var myImage = document.getElementById('image');
myImage.src = elem.value;
}
Basically, the function receives the <input> @type=file as a parameter. It then selects the image element and sets its source to the value of the received element. Nice and simple. I tried this solution in FireFox… and, oops, it doesn’t work! A little bit of research revealed the following:
"file
Creates a file select control. User agents may use the value of the value attribute as the initial file name."
http://www.w3.org/TR/html401/interact/forms.html#h-17.4.1
Apparently, the value attribute of <input> @type=file should not reveal the path to the selected file on user’s computer. I’ll leave it up to the reader to judge the IE’s implementation.
JavaScript Cross-Browser Implementation (Yeah, Right!):
The remaining 10% of posters kept referring to getAsDataURL(). So, I searched around for getAsDataURL(). It seems to me that various posters are saying that it’s an overall standard and if called on a selected file, supposed to return “the contents of the file as a base64 encoded text”.
http://help.dottoro.com/ljbnqsqf.php
I searched more, but could not find any solid definition for getAsDataURL(). If anyone has more info on it, please share! Let’s try it in action:
function PreviewImg(elem) {
var myImage = document.getElementById('image');
myImage.src = elem.files[0].getAsDataURL();
}
var myImage = document.getElementById('image');
myImage.src = elem.files[0].getAsDataURL();
}
I tried it in FireFox and it worked like a charm! I went back to Internet Explorer… didn’t work! At that point, I pulled up Chrome and to my surprise, it didn’t work in Chrome either.
Conclusion:
Well, I don’t have a solid one. Googling for Chrome specific solution didn’t get me anywhere. Also, I haven’t tested this in Safari and Opera. Here is a quick and nasty piece of code that I put together. I use jQuery here to check for a specific browser (not a good practice). The browser check if statement could be changed to a try/catch kind of deal (also nasty).
function PreviewImg(elem) {
var myImage = document.getElementById('image');
if ($.browser.mozilla) //works in FF
myImage.src = elem.files[0].getAsDataURL();
else if ($.browser.msie) //works in IE 6, 7, 8
myImage.src = elem.value;
}
var myImage = document.getElementById('image');
if ($.browser.mozilla) //works in FF
myImage.src = elem.files[0].getAsDataURL();
else if ($.browser.msie) //works in IE 6, 7, 8
myImage.src = elem.value;
}
If anyone could shine more light on this, please leave a comment. I would also be interested in learning more about getAsDataURL().
Cheers!