first:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script> function upload(input) { //支持chrome IE10 if (window.FileReader) { var file = input.files[0]; filename = file.name.split(".")[0]; var reader = new FileReader(); reader.onload = function() { console.log(this.result) alert(this.result); } reader.readAsText(file); } //支持IE 7 8 9 10 else if (typeof window.ActiveXObject != 'undefined'){ var xmlDoc; xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = false; xmlDoc.load(input.value); alert(xmlDoc.xml); } //支持FF else if (document.implementation && document.implementation.createDocument) { var xmlDoc; xmlDoc = document.implementation.createDocument("", "", null); xmlDoc.async = false; xmlDoc.load(input.value); alert(xmlDoc.xml); } else { alert('error'); } } </script> <title>file upload</title> </head> <body> <input type="file" onchange="upload(this)" /> </body> </html>
two:
<!DOCTYPE html> <html> <head> </head> <body> jsReadFile:<input type="file" onchange="jsReadFiles(this.files)"/> <button onclick="jsReadFiles();">read</button> </body> <script src="./jquery.js"></script> <script> //js 读取文件 function jsReadFiles(files) { if (files.length) { var file = files[0]; var reader = new FileReader();//new一个FileReader实例 if (/text+/.test(file.type)) {//判断文件类型,是不是text类型 reader.onload = function() { $('body').append('<pre>' + this.result + '</pre>'); } reader.readAsText(file); } else if(/image+/.test(file.type)) {//判断文件是不是imgage类型 reader.onload = function() { $('body').append('<img src="' + this.result + '"/>'); } reader.readAsDataURL(file); } } } </script> </html>
随机获取预设定的内容
var msg = ["./a.html","./b.html","./c.html"]; alert((msg[Math.floor(Math.random() * msg.length)]));