Загрузка изображения в Kinvey и Angularjs

Я пытаюсь изменить то, что в настоящее время делает мое приложение, чтобы вместо ввода URL-адреса для ссылки на изображение оно вместо этого загружало изображение в коллекцию Kinvey. Вот JSfiddle того, как я сейчас сохраняю информацию из своей формы в свою коллекцию kinvey.

http://jsfiddle.net/k6MQK/ Вот мой угловой код для сохранения данных формы:

$scope.savePeep = function () {

    var dataObj = angular.copy($scope.peep);
    delete dataObj['$$hashKey'];

    // Add the ad hoc fields to the peep object if they are filled out
    if ($scope.adHocItem) {
        dataObj.adHocLocation = $scope.adHocItem.normalized_location;
        dataObj.adHocLocation_display = $scope.adHocItem.display_location;
    }


    KinveyService.savePeep(dataObj, function (_result) {

        debugger;
        // update local collection
        KinveyService.setCollectionObject(_result.data, $stateParams.peepId);

        $state.transitionTo('home');
    });
};

Я хочу изменить его так, чтобы вместо ввода текста, подобного этому:

   <input type="text" id="menu_url" name="menu_url"
 placeholder="" class="form-control" ng-model="peep.menu_url">

это вход для загрузки файла, который работает.

<input type="file" id="menu_url" name="menu_url"
     placeholder="" class="form-control" ng-model="peep.menu_url">

person Gchorba    schedule 23.05.2014    source источник


Ответы (1)


Простая загрузка файлов с помощью Kinvey и 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>
person Aaron Saunders    schedule 24.05.2014