我正在尝试将照片上传添加到已在Kinvey的收藏中设置的个人帐户中。我大部分时间都在乱搞它并且无法解决这个问题。任何人对我应该如何设置有任何建议? 谢谢!
编辑:
所以我研究了如何做更多,Kinvey说要像这样构建它:var fileContent = 'file-content-string';
var promise = $kinvey.File.upload(fileContent, {
_id : 'my-file-id',
_filename : 'my-file.txt',
mimeType : 'text/plain',
size : fileContent.length
});
但是我不清楚如何将其实现到我的角度代码中。我正在使用ng-file-upload,所以我的角度代码看起来像这样:
var MyCtrl = [ '$scope', '$upload', function($scope, $upload) {
$scope.onFileSelect = function($files) {
//$files: an array of files selected, each file has name, size, and type.
for (var i = 0; i < $files.length; i++) {
var file = $files[i];
$scope.upload = $upload.upload({
url: 'server/upload/url', //upload.php script, node.js route, or servlet url
data: {myObj: $scope.myModelObj},
file: file,
}).progress(function(evt) {
console.log('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
}).success(function(data, status, headers, config) {
// file is uploaded successfully
console.log(data);
});
}
};
}];
如何将这些结合起来使其发挥作用? 感谢。
答案 0 :(得分:2)
使用Kinvey&amp; amp ;;进行简单文件上传AngularJS http://bit.ly/1ncdQLq
<!DOCTYPE html>
<html>
<head>
<title>Kinvey File Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<script src="https://da189i1jfloii.cloudfront.net/js/kinvey-angular-1.1.4.min.js"></script>
</head>
<body ng-app="kinveyUploadApp" ng-controller="MainCtrl">
<input type="file" id="files" name="files[]" />
<p ng-if="fileModel">
File Size: {{fileModel.size}} Last Modified: {{fileModel['_kmd'].lmt | date:'yyyy-MM-dd HH:mm:ss Z'}}
</p>
<script>
angular.module('kinveyUploadApp', ['kinvey'])
.run(['$kinvey', function ($kinvey) {
// Kinvey initialization starts
var promise = $kinvey.init({
appKey: 'appKey',
appSecret: 'appSecret'
});
promise.then(function () {
// Kinvey initialization finished with success
console.log("Kinvey init with success");
}, function (errorCallback) {
// Kinvey initialization finished with error
console.log("Kinvey init with error: " + JSON.stringify(errorCallback));
});
}])
.controller('MainCtrl', ['$scope', '$kinvey', function ($scope, $kinvey) {
$scope.fileModel = {};
angular.element(document).find('input')[0].addEventListener('change', function (e) {
var theFile = e.target.files[0];
var promise = $kinvey.File.upload(theFile, {
_filename: theFile.name,
public: true,
size: theFile.size,
mimeType: theFile.type
}).then(function (_data) {
console.log("[$upload] success: " + JSON.stringify(_data, null, 2));
$scope.fileModel = _data;
}, function error(err) {
console.log('[$upload] received error: ' + JSON.stringify(err, null, 2));
});
}, false);
}]);
</script>
</body>
答案 1 :(得分:0)
var promise = $kinvey.File.upload(files[0].file,{
_filename: files[0].file.name,
public: true,
size: files[0].file.size,
mimeType: files[0].file.type
}).then(function (_result) {
console.log("$upload: " + JSON.stringify(_result));
//file.cancel()
}, function error(err) {
console.log('[$upload] received error: ' + JSON.stringify(err));
});
return promise;
});