在Javascript(Angular)中读取,更改和保存文档 [英] Read, change and save doc in Javascript (Angular)

查看:82
本文介绍了在Javascript(Angular)中读取,更改和保存文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to read file (.doc) then replace some data in doc and then send to print (doc or pdf).

第一步我尝试从文档中读取数据。从.txt起作用,但来自.doc no :(

At first step I try to read data from docs. From .txt its work, but from .doc no :(

我在jsfiddle http://jsfiddle.net/qo0fxo50/

I did example in 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>

和指令(on-read-file):

And directive (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/

I did example in jsfiddle http://jsfiddle.net/qo0fxo50/

推荐答案

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

.doc is a proprietary and binary format (also there are multiple incompatible versions). That means it's a bunch of undocumented bytes instead of some charackters as with .txt.

除非你了解细节,否则你不会得到任何东西。该格式或找到一个可以帮助您阅读它的库。我建议将'.doc-contents自动化为你可以解析的东西 - 用工具进行转换(应该有一些东西,但不要考虑准确的结果),或者更好的是不要使用.doc。

You won't get anything out of it unless you understand the details of that format or find a library that helps you to read it. I'd suggest automate '.doc-contents to something you can parse'-conversion with a tool (there should be something out there, but don't exspect accurate results) or even better don't use .doc.

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

As for the new .docx formats it should be a little easier to get contents, as they are basically .xml.

这篇关于在Javascript(Angular)中读取,更改和保存文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆