zoukankan      html  css  js  c++  java
  • js 读本地文件

    http://www.jb51.net/article/21191.htm

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js 读本地文件</title> <script src="jquery.js"></script> </head> <body> <input type="file" id="data" name=""> <a href="#" onclick="load()">Load</a> <br/> <div id="editor" contentEditable="true" style="height: 250px;"></div> <script> function load() { var finput = document.getElementById("data"); // var editor = document.getElementById("editor"); var f = finput.files[0]; if(f){ var r = new FileReader(); r.onload = function(e){ var result = e.target.result; if(result){ var errMsg = ''; var codeList = []; var lines = result.split(' '); for(var i=0; i<lines.length; i++){ var line = lines[i].split(','); if(line[0] != '' && line[1] != ''){ codeList.push({"code": line[0],"weight": line[1]}) }else{ errMsg += ('数据格式不正确: ' + line); } } if(codeList.length > 0){ show(codeList); } if(errMsg != ''){ alert(errMsg); } } } r.readAsText(f); } // else{ // editor.InnerHTML = "Failed to load file"; // } } function show(codeList){ for(var i=0; i<codeList.length;i++){ console.log('line ' + i + ' code: ' + codeList[i].code + ' weight: ' + codeList[i].weight); } } </script> </body> </html>
  • 相关阅读:
    break return continue
    爬虫---请求
    pycharm加开头注释
    爬虫---入门
    pip
    XML基础
    英语
    布局
    adobe
    StackOverflow
  • 原文地址:https://www.cnblogs.com/james641/p/8986406.html
Copyright © 2011-2022 走看看