2016-08-25 165 views
1

我沒有太多的前端經驗,所以請耐心等待。在瀏覽器中加載JSON文件

我在瀏覽器中有一個本地的Angular應用程序(無後端),我想上傳一個JSON文件,以便我可以在圖上顯示上傳的數據。由於我不知道Angular的fs.readFile是否可以,我選擇了ng-file-upload以很好的方式(從文件瀏覽器)加載文件。讓我知道,如果它不是正確的工具,但...

所以我已經從官方JS Fiddle on the repo複製代碼。不得不稍微修改它以允許我上傳任何內容 - 刪除驗證。

var app = angular.module('app', ['ngFileUpload']); 

app.controller('fileUploader', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) { 
    $scope.uploadFiles = function(file, errFiles) { 
    $scope.f = file; 
    $scope.errFile = errFiles && errFiles[0]; 
    if (file) { 
     file.upload = Upload.upload({ 
     url: 'https://angular-file-upload-cors-srv.appspot.com/upload', 
     data: {file: file} 
     }); 

     file.upload.then(function (response) { 
     $timeout(function() { // don't know why this timeout 
     $scope.fileReady = true; 
     file.result = response.data; 

     // can't figure out what to do with this!!! 
     console.log('response.data:\n', JSON.stringify(response.data.result, null, 2)); 
     }); 
     }, function (response) { 
     if (response.status > 0) 
      $scope.fileError = response.status + ': ' + response.data; 
     }); 
    } 
    }; 
}]); 

的反應出來作爲一個對象,我不能做與:

[ 
    { 
    "fieldName": "file", 
    "name": "my-awesome-data.json", 
    "size": "364077" 
    } 
] 

我如何獲得已上載的實際數據/解析JSON文件?

+1

是啊,也許是'NG-文件uploader'不是本地文件的工具。這是一個更簡單的解決方案:http://stackoverflow.com/questions/26165919/filereader-not-loading-file-properly-using-angularjs – sakovias

回答

2

試圖讓使用$ http.get像文件內容:

$http.get(response.data).success(function(data) { 
    console.log(data); 
});