javascript - getAsDataURL() doesn't work in chrome and ie - Stack Overflow

This is my HTML:<input type="file" id="browse" name="browse" size=&quo

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:

  1. onclick doesn't work in Chrome and

  2. getAsDataURL() doesn't work in Chrome and IE

Can you help me?

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:

  1. onclick doesn't work in Chrome and

  2. getAsDataURL() doesn't work in Chrome and IE

Can you help me?

Share Improve this question edited Nov 8, 2011 at 13:38 Some Guy 16.2k10 gold badges60 silver badges68 bronze badges asked Nov 8, 2011 at 12:57 Aram MkrtchyanAram Mkrtchyan 2,7004 gold badges35 silver badges48 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

IE does not yet support the File API. Anyhow, you need to use a FileReader to 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/fKQDL/

file = fileObj.files[0];
var fr = new FileReader;
fr.onloadend = changeimg;
fr.readAsDataURL(file);

Bind the button's behavior with jQuery

jQuery('input[type="button"].unknown').click ( onbrowse );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745644594a4637893.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信