尝试使用javascript解析rtf文件
我有一个被上传的rtf文件,一旦上传,我希望它被解析并转换成json文件。我用的大多是角度。我的上传工作得很好,只是解析有问题。该文件确实得到了清理,但没有正确格式化为json文件。有人对最好的做法有什么建议吗尝试使用javascript解析rtf文件,javascript,angularjs,json,Javascript,Angularjs,Json,我有一个被上传的rtf文件,一旦上传,我希望它被解析并转换成json文件。我用的大多是角度。我的上传工作得很好,只是解析有问题。该文件确实得到了清理,但没有正确格式化为json文件。有人对最好的做法有什么建议吗 this.parseFile=function(file, uploadUrl){ reader.onload = function(onLoadEvent){ $rootScope.$apply(function(){ var dat
this.parseFile=function(file, uploadUrl){
reader.onload = function(onLoadEvent){
$rootScope.$apply(function(){
var dataURL = reader.result;
setUpData(dataURL);
});
};
reader.readAsText(file);
};
function saveFile(file){
var fd = new FormData();
fd.append('save_file', file);
$http({
method: 'POST',
url: "includes/loadFile.php",
data: fd,
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function (data, status, headers, config) {
msg = {
title: "success",
cls: "success",
msg: data
};
popUp(msg);
})
.error(function (data, status, header, config) {
msg = {
title: "error",
cls: "danger",
msg: data
};
popUp(msg);
});
};
function setUpData(data)
{
text = data;
text = text.replace('/[\p{Z}\s]{2,}/u', ' ');
// skip over the heading stuff
j= text.indexOf('{',1); // skip ahead to the first part of the header
var loc = 1;
var t ="";
var ansa="";
len = text.length;
getpgraph(); // skip by the first paragrap
while(j<len) {
c = text.substr(j,1);
if (c=="\\") {
// have a tag
var tag = gettag();
if (tag.length > 0) {
// process known tags
switch (tag) {
case 'par':
ansa+="\r\n";
break;
// ad a list of common tags
// parameter tags
case 'spriority1':
case 'fprq2':
case 'author':
case 'operator':
case 'sqformat':
case 'company':
case 'xmlns1':
case 'wgrffmtfilter':
case 'pnhang':
case 'themedata':
case 'colorschememapping':
var tt = gettag();
break;
case '*':
case 'info':
case 'stylesheet':
// gets to end of paragraph
j--;
getpgraph();
default:
// ignore the tag
}
}
} else {
ansa += c;
}
j++;
};
ansa = ansa.replace(/{|}|"|( | )\r/g,'');
ansa = ansa.replace(/Amount After|Amt After/g,'');
ansa = ansa.replace(/negetive amount means Refund NOTE: Layaways are not in Sales Report till they're paid off/g,'');
ansa = ansa.replace("On Line Vendor Sales Quick Report by Month",'');
ansa = ansa.replace(/ ( ) /g,'');
ansa.trim();
ansa.split("\n");
console.log(ansa);
var newData = new Array();
var vendor = new Array();
var orders = new Array();
var v = 0;
//Lets look for the line that has the word vendor
vendorLine = ansa.match(/Vendor/gi);
//Look for each order for the vendor
orderLine = ansa.match(/date/gi);
figuresLine = ansa.match(/figures/g);
var start;
var end;
for(arr = 0; arr <=ansa.length; arr++)
{
}
if(vendorLine)
{
vendorInfo = ansa.split(' ',ansa.replace('/Vendor|\r|\s/','') );
vendorFName = vendorInfo[0];
vendorLName = vendorInfo[1];
vendorID = vendorInfo[2];
vendor = {vendorid: vendorID, vendorInfo:{fname: vendorFName,lname:vendorLName} , orders:orders};
}
if( orderLine )
{
start = 1;
}
if( figuresLine )
{
end = 0;
}
if( !end || !start)
{
if( end < start )
{
ordersLine = explode(" ",v);
orderDesc = new Array();
//Remove the stuff we dont need. Like Qty ,Subtotal and Inv#. Once we remove them we reindex them
unset(ordersLine[0],ordersLine[2],ordersLine[count(ordersLine)]); // Removes Subtotal and Inv#
unset(ordersLine[count(ordersLine)]); // Removes QTY
ordersLine = array_values(ordersLine);
orderDate = ordersLine[0];
orderAmt = end(ordersLine);
/*foreach (ordersLine as key=>value)
{
if(key != 0)
{
if(key != count(ordersLine)-1)
{
array_push(orderDesc,value);
orderLines = array("vendorid"=>vendorID,"order"=>array("date"=> orderDate,"desc"=> implode(" " ,orderDesc), "amount"=>orderAmt) );
}
};
}*/
}
}
vendor['orders'] = orders;
saveFile(ansa);
}
function gettag() {
// gets the text following the / character or gets the param if it there
var tag='';
while(true) {
j++;
if (j>=len) break;
c = text.substr(j,1);
if (c==';') break;
if (c=='}') break;
if (c=="\\") {
j--;
break;
}
if (c=="{") {
//getpgraph();
break;
}
if (((c>='0')&&(c<='9'))||((c>='a')&&(c<='z'))||((c>='A')&&(c<='Z'))||c=="'"||c=="-"||c=="*" ){
tag= tag+c;
} else {
// end of tag
j--;
break;
}
}
return tag;
}
function getpgraph() {
// if the first char after a tag is { then throw out the entire paragraph
// this has to be nested
var nest = 0;
while(true) {
j++;
if (j>=len) break;
if (text.substr(j,1)=='}') {
if (nest==0) return;
nest--;
}
if (text.substr(j,1)=='{') {
nest++;
}
}
return;
}
this.parseFile=函数(文件,上传URL){
reader.onload=函数(onload事件){
$rootScope.$apply(函数(){
var dataURL=reader.result;
setUpData(dataURL);
});
};
reader.readAsText(文件);
};
函数saveFile(文件){
var fd=新FormData();
fd.append('save_file',file);
$http({
方法:“POST”,
url:“includes/loadFile.php”,
数据:fd,
请求:angular.identity,
标题:{'Content-Type':未定义}
})
.success(函数(数据、状态、标题、配置){
味精={
标题:“成功”,
cls:“成功”,
msg:数据
};
弹出窗口(msg);
})
.错误(功能(数据、状态、标题、配置){
味精={
标题:“错误”,
cls:“危险”,
msg:数据
};
弹出窗口(msg);
});
};
功能设置数据(数据)
{
文本=数据;
text=text.replace('/[\p{Z}\s]{2,}/u','';
//跳过标题
j=text.indexOf('{',1);//向前跳到标题的第一部分
var-loc=1;
var t=“”;
var ansa=“”;
len=text.length;
getpgraph();//按第一段跳过
while(j0){
//处理已知标签
开关(标签){
案例“par”:
ansa+=“\r\n”;
打破
//添加常用标记的列表
//参数标签
案例“精神1”:
案例“fprq2”:
案例“作者”:
“操作员”案例:
案例“sqformat”:
“公司”一案:
案例“xmlns1”:
案例“wgrffmtfilter”:
个案"pnhang":
案例“主题数据”:
案例“colorschememapping”:
var tt=gettag();
打破
案例“*”:
案例“信息”:
案例“样式表”:
//到达段落末尾
j--;
getpgraph();
违约:
//忽略标签
}
}
}否则{
ansa+=c;
}
j++;
};
ansa=ansa.replace(/{124;}|“|(|))\ r/g';
ansa=ansa.替换(/Amount在| Amount在/g之后,”);
ansa=ansa.replace(/negetive AMENT表示退款说明:未付款项在付清之前不在销售报告中/g');
ansa=ansa.replace(“按月在线供应商销售快速报告”);
ansa=ansa.替换(/()/g');
安萨·特里姆();
ansa.split(“\n”);
控制台日志(ansa);
var newData=newarray();
var vendor=新数组();
var orders=新数组();
var v=0;
//让我们查找包含单词vendor的行
vendorLine=ansa.match(/vendorLine/gi);
//查找供应商的每个订单
orderLine=ansa.match(/date/gi);
figuresLine=ansa.match(/figures/g);
var启动;
var端;
用于(arr=0;arr值)
{
如果(键!=0)
{
如果(键!=计数(订单行)-1)
{
数组_push(orderDesc,值);
orderLines=array(“vendorid”=>vendorid,“order”=>array(“date”=>orderDate,“desc”=>introde(“,orderDesc),“amount”=>orderAmt));
}
};
}*/
}
}
供应商['orders']=订单;
保存文件(ansa);
}
函数gettag(){
//获取/字符后面的文本,或获取参数(如果有)
var标签=“”;
while(true){
j++;
如果(j>=len)中断;
c=文本。substr(j,1);
如果(c==';')中断;
如果(c=='}')中断;
如果(c==“\\”){
j--;
打破
}
如果(c==“{”){
//getpgraph();
打破
}
如果(((c>='0')&&(c='a')&&(c='a')&&(c=len)中断;
if(text.substr(j,1)='}'){
如果(nest==0)返回;
巢--;
}
如果(text.substr(j,1)='{'){
nest++;
}
}
返回;
}
我所期望的是创建一个干净、格式良好的JSON文件,然后在用户所在的页面上显示给用户。基本上用新上传的解析数据刷新页面。有一个JS RTF解析器可能会有所帮助。然后,您需要对结果进行JSON编码
不确定这是否回答了您的问题,但更多的信息可能有助于锁定修复。我们需要一些关于您一直在做什么的代码,以及文件的更多详细信息以及结果应该是什么样的