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

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

问题描述

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

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

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

我尝试这样做:

选择文件

<input type="file" on-read-file="showContent($fileContent)"/><div><h2>文件内容是:</h2><pre>{{ contentfile }}</pre>

和指令(读取文件):

directives.directive('onReadFile', function ($parse) {返回 {限制:'A',范围:假,链接:函数(范围,元素,属性){var fn = $parse(attrs.onReadFile);element.on('change', function(onChangeEvent) {var reader = new FileReader();reader.readAsText((onChangeEvent.target).files[0], 'CP1251');reader.onload = 函数(onLoadEvent){范围.$应用(函数(){fn(scope, {$fileContent:onLoadEvent.target.result});});};});}};});

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

解决方案

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

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

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

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

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

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

I try to do it like:

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

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


                });
            }
        };
    });

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

解决方案

.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.

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.

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天全站免登陆