2014-12-19 48 views
0

我需要讀取文件(.doc),然後替換doc中的一些數據,然後發送到打印文檔(doc或pdf)。使用Javascript(Angular)閱讀,更改和保存文檔

第一步我嘗試從文檔中讀取數據。從.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/

回答

2

.DOC做例子是專有和二進制格式(也有多個不兼容的版本)。 這意味着它是一堆無證字節,而不是像.txt一樣的一些字符。

除非您瞭解該格式的詳細信息或找到幫助您閱讀該格式的庫,否則將不會獲得任何內容。我會建議自動化'.doc內容到你可以解析的東西' - 用一個工具進行轉換(應該有些東西在那裏,但不要期待準確的結果),或者更好的是不要使用.doc。

至於新的.docx格式,它應該更容易獲取內容,因爲它們基本上是.xml。