user3646131
user3646131

Reputation:

Upload image to Kinvey with Angular

Im trying to add photo uploading to individual accounts that are already set up in a collection in Kinvey. I've been messing around with it most of the day and can't figure it out. Anyone have any suggestions on how I should set this up? thanks!

Edit:

So i looked into how to do it more and Kinvey says to structure it like so: 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 });

however I am unclear how to implement that into my angular code. I am using ng-file-upload so my angular code should look something like this:

               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);
      });
    }
  };
}];

How how can I combine these to make it work? thanks.

Upvotes: 1

Views: 442

Answers (2)

Aaron Saunders
Aaron Saunders

Reputation: 33345

Simple File Upload with 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>

Upvotes: 2

Gchorba
Gchorba

Reputation: 301

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;

        });

Upvotes: 0

Related Questions