This is my HTML:
<input type="file" id="browse" name="browse" size="" placeholder="Photo" checked="checked" class="upload"/>
<input type="button" onclick="javascript:onbrowse()" class="unknown" value=""/>
And my JavaScript:
$(function() {
$(".upload").change(function () {
var fileObj = $(this).get(0);
var fileName;
if (fileObj.files) {
fileName = fileObj.files.item(0).getAsDataURL()
} else {
fileName = fileObj.value;
}
$(".unknown").css("background-size", "100px 100px");
$(".unknown").css("background-image", "url(" + fileName + ")");
});
});
function onbrowse() {
document.getElementById('browse').click();
}
I have two problems:
-
onclick doesn’t work in Chrome and
-
getAsDataURL() doesn’t work in Chrome and IE
Can you help me?
IE does not yet support the File API. Anyhow, you need to use a
FileReaderto read a file. Also, the file is not its file name (your variable naming is a little ambiguous).The click delegation to the file input works just fine.
http://jsfiddle.net/fKQDL/