【发布时间】:2016-08-26 01:27:32
【问题描述】:
我没有太多前端经验,所以请多多包涵。
我在浏览器中有一个本地 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 文件的实际数据?
【问题讨论】:
-
是的,也许
ng-file-uploader不是本地文件的正确工具。这是一个更简单的解决方案:stackoverflow.com/questions/26165919/…