【问题标题】:Read, change and save doc in Javascript (Angular)在 Javascript (Angular) 中读取、更改和保存文档
【发布时间】:2014-12-19 09:58:21
【问题描述】:

我需要读取文件(.doc),然后替换 doc 中的一些数据,然后发送到打印(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>

And 指令(on-read-file):

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/做了例子

【问题讨论】:

    标签: javascript angularjs angularjs-directive


    【解决方案1】:

    .doc 是专有的二进制格式(也有多个不兼容的版本)。 这意味着它是一堆未记录的字节,而不是 .txt 中的一些字符。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-09
      • 1970-01-01
      • 2011-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多