我正在尝试使用输入类型文件将一个 ofx 文件放入 javascript 代码中,并将其显示在屏幕上以进行财务控制。我的问题是将值与 ofx 分开获取,将其解析为 xml 或将其分隔为 json。
起初我只尝试使用 javascript,但很快我意识到我会更好地通过 php 传递它。也许我错了,如果有一种简单的方法可以做到这一点,我愿意接受改变。
<body>
<div class="topo">
<a style="color: white" href="">
<b>Home</b>
</a>
</div>
<div>
<div id="drop-area">
Upload OFX
<input type="file" id="upload" multiple onchange="handleFiles(this.files)">
</div>
</body>
var reader = new FileReader();
let getxml = function(path, callback){
let request = new XMLHttpRequest();
request.open("GET", path);
request.setRequestHeader("Content-Type", "text/xml");
request.onReadystatechange = function(){
if (request.readyState === 4 && request.status === 200){
callback(request.responseXML);
}
}
request.send();
}
getxml('ofxs/teste.ofx', function(xml){
console.log(xml);
})
<?php
print_r($_FILES);
$headers = array();
$charsets = array(
1252 => 'WINDOWS-1251',
);
while(!feof($_FILES)) {
$line = trim(fgets($_FILES));
if ($line === '') {
break;
}
list($header, $value) = explode(':', $line, 2);
$headers[$header] = $value;
}
$buffer = '';
// dead-cheap SGML to XML conversion
// see as well http://www.hanselman.com/blog/PostprocessingAutoClosedSGMLTagsWithTheSGMLReader.aspx
while(!feof($_FILES)) {
$line = trim(fgets($_FILES));
if ($line === '') continue;
$line = iconv($charsets[$headers['CHARSET']], 'UTF-8', $line);
if (substr($line, -1, 1) !== '>') {
list($tag) = explode('>', $line, 2);
$line .= '</' . substr($tag, 1) . '>';
}
$buffer .= $line ."\n";
}
// use DOMDocument with non-standard recover mode
$doc = new DOMDocument();
$doc->recover = true;
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
$save = libxml_use_internal_errors(true);
$doc->loadXML($buffer);
libxml_use_internal_errors($save);
echo $doc->saveXML();
?>
我真的不知道自己在做什么,我只是按照stackOV的一些tutos和答案而被困在这里。'-'