在Javascript(Angular)中读取,更改和保存文档

Read, change and save doc in Javascript (Angular)

本文关键字:保存 文档 读取 Javascript Angular      更新时间:2023-09-26

我需要读取文件(.doc(,然后替换文档中的一些数据,然后发送到打印(文档或pdf(。

第一步,我尝试从文档中读取数据。从.txt它的工作,但从.doc没有:(

我在 jsfiddle http://jsfiddle.net/qo0fxo50/中做了例子

我尝试这样做:

<h1>Select file</h1>
    <input type="file" on-read-file="showContent($fileContent)" />
    <div>
        <h2>File content is:</h2>
        <pre>{{ contentfile }}</pre>
    </div>

和指令(读取文件(:

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});
                        });
                    };

                });
            }
        };
    });

我在 jsfiddle 中做了例子 http://jsfiddle.net/qo0fxo50/

.doc是一种

专有的二进制格式(也有多个不兼容的版本(。这意味着它是一堆未记录的字节,而不是像.txt那样的一些聊天者。

除非您了解该格式的详细信息或找到可以帮助您阅读它的库,否则您将无法从中获得任何内容。我建议使用工具自动将".doc内容转换为可以解析的内容"转换(应该有一些东西,但不要检查准确的结果(,甚至最好不要使用.doc。

至于新的.docx格式,获取内容应该更容易一些,因为它们基本上是.xml的。