<!DOCTYPE html>
<html lang="en">
<head>
<title> </title>
<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
<body>
请选取一个图像文件: <input type = "file" id = "file" name = "file" />
<div id = "result"> </div>
</body>
</html>
<script type = "text/javascript" >
/*
uploadBtn: string 点击上传的input按钮的id
返回图片通过base64转换后的url
*/
$("#file").on("change",function(e){
var file = e.target.files[0];
if (file) {
var reader = new FileReader();
reader.onabort = function ( event ) {console.log("中断")};
reader.onerror = function ( event ) {console.log("出错")};
reader.onloadstart = function ( event ) {console.log("开始")};
reader.onprogress = function ( event ) {console.log("正在读取")};
reader.onload = function ( event ) {console.log("成功读取")};
reader.onloadend = function ( event ) {console.log("读取完成,无论成功失败")}
reader.onload = function ( event ) {
var txt = event.target.result;
$("#result").html(txt);
$("#result").append('<img src="'+txt+'">');
return event.target.result;
};
}
reader.readAsDataURL( file );
})
网友评论