开发者

File input problem in HTML5 on Chrome

I'm trying to make a audio player in Chrome with HTML5 and Javascript. But I got an error(file not readable) at the very beginning...

Below is my HTML code:

<head>
    <meta charset="utf-8" />

    <!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
    Remove this if you use the .htaccess -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

    <title>index</title>
    <meta name="description" content="" />
    <meta name="generator" content="Studio 3 http://aptana.com/" />
    <meta name="author" content="liuuzyan" />

    <!--<meta name="viewport" content="width=device-width; initial-scale=1.0" />-->

    <!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
    <link rel="shortcut icon" href="/favicon.ico" />
    <link rel="apple-touch-icon" href="/apple-touch-icon.png" />

    <script src="js/jquery-1.6.1.min.js" type="text/javas开发者_运维技巧cript"></script>
    <script src="js/fun.js" type="text/javascript"></script>
</head>

<body>
    <audio id="player" controls="controls">
        Please use Firefox/Chrome/IE9 to browse this page.
    </audio>
    <input type="file" multiple="multiple" id="fileChose" />
</body>

And below is my Javascript code:

$(document).ready( function() {
    $('#fileChose').change( function() {
        var fileList=this.files;
        for(var i=0;i<fileList.length;i++) {
            var reader=new FileReader();
            reader.onloaded=function(e){
                $('#player').attr('src',e.target.result)
            }
            reader.onerror=function(e){
                switch(e.target.error.code) {
                    case e.target.error.NOT_FOUND_ERR:
                            alert("file not found");
                            break;
                    case e.target.error.NOT_READABLE_ERR:
                            alert("file not readable");
                            break;
                    case e.target.error.ABORT_ERR:
                            alert("aborted");
                            break;
                    default:
                            alert('generic error?');
                }
            }
            reader.readAsDataURL(fileList[i]);
        }
    });
});

Can anyone help me with this problem? Thanks a lot!


In Chrome you can add: --allow-file-access-from-files to the Chrome command line to allow local file to be read from local HTML pages.

You can also bundle your code up as Chrome Extension and get local file access that way.

Or upload the HTML files to a Server as already mentioned.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜