有没有简单一点的方案,因为要读取上传的TXT文件,并且还要对读取的文件内容进行判断。
2条回答 默认 最新
_雪菜肉丝面_ 2022-03-07 12:36关注<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script> </head> <body> <div> 上传文件 : <input type="file" name="file" id="fileId"/> <button type="submit" name="btn" id="btnId" onclick="check()">提交</button> 提交 </div> <script> function check() { var objFile = document.getElementById("fileId"); if (objFile.value === "") { alert("不能空") } console.log(objFile.files[0].size); // 文件字节数 var files = $('#fileId').prop('files');//获取到文件列表 if (files.length === 0) { alert('请选择文件'); } else { var reader = new FileReader();//新建一个FileReader reader.readAsText(files[0], "UTF-8");//读取文件 reader.onload = function (evt) { //读取完文件之后会回来这里 var fileString = evt.target.result; // 读取文件内容 console.log(fileString) } } } </script> </body> </html>解决 无用评论 打赏 举报