源代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
img,a,textarea {
display: block;
width: 1000px;
height: 500px;
}
</style>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
</head>
<body>
<br/>
<br/>
<input type="file" id="txt" onchange="uploadfile()">
<br/>
<br/>
<textarea id="textarea_id"></textarea>
<script>
function uploadfile(){
var file=$("#txt")[0].files[0];
//判断上传文件是不是txt格式,判断后缀是不是.txt
if(file.name.substr(-4).toLocaleLowerCase() != '.txt'){
alert("请上传格式为txt的文件!");
windows.location="test.jsp";//重新定位到上传txt文件页面
}
else//如果上传文件是txt文件,则显示文件的预览
{
var reader=new FileReader;
reader.readAsText(file,'utf-8');
//reader.readAsDataURL(file);
reader.onload=function(evt){
var data=evt.target.result;
console.log("data",evt.target)
$('#textarea_id').val(data);
}
}
}
</script>
</body>
</html>
网友评论