2016-07-19 34 views
0

我想弄清楚如何讀取上傳到我的WebApp的文本/ XML文件的內容。在這一點上,我不想/不需要擊中服務器,我只是想獲取文件的內容/文本。角度上傳文本文件,然後獲取內容

這是我迄今所做的: xmlDiff.html:

<div id="xmlDiff-div" class="wrapper"> 
    <div class="configurationView "> 
     <div class="panelHeader">Upload XML Files</div> 
     <div class="treeWrapper panelBody"> 
      <button ngf-select="uploadLeftFile($file)" accept="xml/*" ngf-max-height="1000" ngf-max-size="2MB"> 
       Upload Left File</button> 
      <br><br> 
      <button ngf-select="uploadRightFile($file)" accept="xml/*" ngf-max-height="1000" ngf-max-size="2MB"> 
       Upload Right File</button> 
      <br><br> 
     </div> 
    </div> 
    <div id="diff_editors_div"> 
     <div class="panelheader sectionHeader">XML Diff Results</div> 
     <div id="compare"></div> 
    </div> 
</div> 

現在,這裏是我的控制器:

app.controller('XmlDiffCtrl', ['$scope', '$q', '$location', '$timeout', function ($scope, $q, $location, $timeout) { 
    $('#compare').mergely({ 
     cmsettings: {readOnly: false, lineNumbers: true}, 
     ignorews: true, 
     width: 'auto', 
     height: 'auto', 
     lhs: function (setValue) { 
      setValue('paste left XML here'); 
     }, 
     rhs: function (setValue) { 
      setValue('paste right XML here'); 
     } 
    }); 
    $scope.uploadRightFile = function (file) { 
     console.log("Upload Right File has been called"); 
     console.log("file content :" + file); 
     $scope.rightFileText = file.data; **doesn't work, need an alternative** 

    }; 
    $scope.uploadLeftFile = function (file) { 

    }; 
}]); 

正如一個音符,我是角初學者開發者,所以如果任何人想要打擊我的Angular技能,但我主要需要幫助解決如何在上傳後訪問文件的內容。

讓我知道如果我沒有足夠清楚,如果你們都需要更多的細節。

+0

[的FileReader(https://developer.mozilla.org/en/docs/Web/API/FileReader)可能? –

+0

當你說你想要「獲取」文件的內容時,你的意思是你想在它被選中後顯示在屏幕上嗎?就像顯示選定文件的縮略圖一樣? –

+0

是的,我真的想在合併編輯器中顯示它,但基本上你在說什麼是的。 –

回答

0

試試這個:

function handleFiles(file) { 
    var reader = new FileReader(); 
    reader.onload = function(e) { 
    $scope.rightFileText = e.target.result; } 
    reader.readAsDataURL(file); 

} 
+0

e.target.result究竟是什麼? –

+0

試過了你說的話,我得到了明顯的錯誤,因爲'e'不存在,並且: 'code' angular.js:12416錯誤:無法在'FileReader'上執行'readAsDataURL':對象已經在忙讀斑點。 xmlDiffController.js:22 Uncaught ReferenceError:e未定義 'code' –

+0

你看到我剛編輯過嗎?或者您可以嘗試使用「this.result」 –

相關問題