jsローカルディスクテキストファイルを読み込んでJSONデータ(フォーマット付きテキスト)として保存

3568 ワード

          ,HTML5             
//           

    $(".myappTXTUploadBtn").click(function(){



        var filepath = $(".uploadFace").val().trim();

        if(filepath == ""){

            showBlackPage("        txt  ","        txt  ");

            return;

        }

        var last = filepath.substr(filepath.lastIndexOf(".")+1);

        if(last == "txt"){

            var file = $(".uploadFace")[0].files[0];

            var reader = new FileReader();

            reader.readAsText(file, "GB2312");

            var urlData;

            reader.onload = function (e) {

                urlData = this.result;

                var dataarray = urlData.split("
"); var objs = "["; var index = 1; for(var i=0;i<dataarray.length;i++){ var data = dataarray[i].trim(); if(data == ""){ continue; }else{ var namestart = data.indexOf("【"); var nameend = data.lastIndexOf("】"); var str = ""; if(namestart != -1 && nameend != -1){ var obj = {}; obj.replytxt = data.substr(namestart+1,nameend-1); str += data.substr(namestart+1,nameend-1); var imgstart = data.indexOf("〖"); var imgend = data.lastIndexOf("〗"); if(imgstart != -1 && imgend != -1){ obj.receiveimg = data.substr(imgstart+1,imgend-nameend-2); str += "
"+data.substr(imgstart+1,imgend-nameend-2); obj.receivetxt = data.substr(imgend+1); str += "
"+data.substr(imgend+1); }else{ obj.receivetxt = data.substr(nameend+1); str += "
"+data.substr(nameend+1); } obj.myappid = index; index++; objs += JSON.stringify(obj)+","; }else{ continue; } } } objs = objs.substr(0,objs.lastIndexOf(",")); objs += "]" if(weixinid != ""){ $.ajax({ type:"POST", url:"/api2/app/myappadd?", data:{ weixinid:weixinid, "myapp":objs }, success:function(serverData){ showBlackPage(serverData[" "],serverData[" "]); } }); }else{ alert(" "); } } }else{ alert(" txt "); } });