在Javascript中读取、更改和保存文档(角度)
我需要读取文件(.doc),然后替换文件中的一些数据,然后发送打印(doc或pdf) 在第一步中,我尝试从文档中读取数据。从.txt开始工作,但从.doc编号:( 我在jsfiddle中做了一个示例 我试着这样做:在Javascript中读取、更改和保存文档(角度),javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,我需要读取文件(.doc),然后替换文件中的一些数据,然后发送打印(doc或pdf) 在第一步中,我尝试从文档中读取数据。从.txt开始工作,但从.doc编号:( 我在jsfiddle中做了一个示例 我试着这样做: <h1>Select file</h1> <input type="file" on-read-file="showContent($fileContent)" /> <div> <h2>Fi
<h1>Select file</h1>
<input type="file" on-read-file="showContent($fileContent)" />
<div>
<h2>File content is:</h2>
<pre>{{ contentfile }}</pre>
</div>
我在jsfiddle中做了一个例子。doc是一种专有的二进制格式(还有多个不兼容的版本)。 这意味着它是一堆未记录的字节,而不是像.txt那样的一些字符 除非您了解该格式的详细信息或找到一个库来帮助您阅读,否则您将无法从中获得任何信息。我建议您使用工具将“.doc”内容自动转换为您可以解析的内容(应该有一些内容,但不要查看准确的结果),或者最好不要使用.doc 至于新的.docx格式,应该更容易获取内容,因为它们基本上是.xml
directives.directive('onReadFile', function ($parse) {
return {
restrict: 'A',
scope: false,
link: function(scope, element, attrs) {
var fn = $parse(attrs.onReadFile);
element.on('change', function(onChangeEvent) {
var reader = new FileReader();
reader.readAsText((onChangeEvent.target).files[0], 'CP1251');
reader.onload = function(onLoadEvent) {
scope.$apply(function() {
fn(scope, {$fileContent:onLoadEvent.target.result});
});
};
});
}
};
});