html5文件上传

<!doctype html>
<html>
<head>
   <title>文件上传</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <meta name="description" content="">
 <meta name="viewport" content="width=device-width, initial-scale=1.0,minimum-scale=1.0, maximum-scale=1.0,user-scalable=no">
</head>
<body>

<script>
    function readLocalFile () {
        //alert("start...");
        var localFile = document.getElementById("uploadFile").files[0];
        //alert(uploadFile);
        //alert(localFile.name);
        //alert(localFile.type);
        //alert(localFile.size);
        var reader = new FileReader();
        //Uncaught SyntaxError:Unexpected identifier
        var content;
        reader.onload = function(event) {
            content = event.target.result;
            document.getElementById("img").src = content;
            document.getElementById("fileContent").value = content;
        }
        reader.onerror = function(event) {
            alert('error')
            //alert("File could not be read! Code " + event.target.error.code);
        }
        //reader.readAsText(localFile,"UTF-8");

        content = reader.readAsDataURL(localFile,"UTF-8");
        //
    }
</script>

<section>
 <input type="file" id="uploadFile" onchange="readLocalFile()";/>
 <div><img src="" id="img"/></div>

 <div><textarea id="fileContent" rows="30" cols="60"></textarea></div>
</section>

</body>
</html>
阅读更多

没有更多推荐了,返回首页