javascript - 将图像上传到 Kinvey 和 Angularjs

标签 javascript angularjs kinvey

我正在尝试更改我的应用程序当前的功能,以便将图像上传到 Kinvey 集合,而不是输入引用图像的 url。 这是我当前如何将表单中的信息保存到我的 kinvey 集合中的 JSfiddle。

http://jsfiddle.net/k6MQK/ 这是我用于保存表单数据的 Angular 代码:

$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">

最佳答案

使用 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>

关于javascript - 将图像上传到 Kinvey 和 Angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23832804/

相关文章:

angularjs - Controller 销毁后 $q notfy 监听器未删除

javascript - 由 jquery.blur() 引起的 Inprog 操作已在进行中

javascript - 如何检测 Angular ng-repeat 是否显示结果

android - 在 Android Studio 中设置 Kinvey

android - 无法根据链接接收对象

javascript - 使用 javascript (thunderbird) 获取消息正文

javascript - If Else 语句没有被执行

xcode - 无法从 kinvey 获取集合中的数据到 TableView Controller

javascript - Node.js - Socket.io 只能通过 localhost 访问

javascript - 通过 javascript 调用更改 onclick 函数时无法发送动态 javascript 参数