js中FileReader对象入门-fileReader中的事件

来源:互联网 发布:java服务器是什么 编辑:程序博客网 时间:2024/05/16 11:31

内容摘自《HTML5应用开发与实践》

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
//事件
function readFile(){
var file=document.getElementById("file").files[0];
var reader=new FileReader();
var result=document.getElementById("result");


reader.onload=function(e){
result.innerHTML='<img src="'+this.result+'" alt=""/>';
alert("文件读取完成时触发");
}


reader.onprogress=function(e){
alert("读取中");
}


reader.onabort=function(e){
alert("中断时触发");
}


reader.onerror=function(e){
alert("出错时触发");
}


reader.onloadstart=function(e){
alert("读取开始时触发");
}


reader.onloadend=function(e){
alert("读取完成触发,无论成功或失败");
}


reader.readAsDataURL(file);
}
</script>
</head>


<body>
<p>
<label>请选择一个文件:</label>
<input type="file"  id="file"/><br />
<input type="button" onclick="readFile();" value="读取文件"/><br />
</p>
<div name="result" id="result"></div>
<!--在这里显示读取结果-->
<br/><br />
</body>
</html>

0 0
原创粉丝点击