0

Simple question, how to create an angularjs upload form to Parse.com?
I am trying to create a profile picture uploader form using using this stackoverflow answer, but it not works

HTML code

<div class="col-lg-9"  ng-controller="manageProfile">
    <form style="margin-top:80px">
        <div class="form-group">
            <label>Browse for Picture</label>
            <input type="file" name="profPic" id="profPic"></input>
        </div>
        <button class="btn btn-md" ng-click="uploadPicture()">Upload Picture</button>
    </form>
</div>

AngularJS code

profile.controller('manageProfile', ['$scope', function($scope) {
$scope.uploadPicture = function() {
    var fileUploadControl = $("#profPic")[0];
    var file = fileUploadControl.file[0];
    var name = file.name;
    var parseFile = new Parse.File(name, file);

    parseFile.save().then(function() {
        var currentUser = Parse.User.current();
        currentUser.set("profilePicture", parseFile);
        currentUser.save();
    }, function(error) {
        alert("Error: " + error.code + ' ' + error.message);
    });
}

I got this error when trying to upload

error: fileuploadcontrol is undefined
3
  • Show the code that you have tried. Commented Mar 27, 2016 at 17:01
  • Also look at parse-angular-patch Commented Mar 27, 2016 at 17:06
  • i am sorry for not writing the code right away... Commented Mar 28, 2016 at 1:50

1 Answer 1

0

In AngularJS, input elements use the ng-model directive to bind input to a scope variable.

<div class="col-lg-9"  ng-controller="manageProfile">
    <form style="margin-top:80px">
        <div class="form-group">
            <label>Browse for Picture</label>
               <!-- add ng-model directive -->
            <input ng-model="fileName" type="file" name="profPic" id="profPic"></input>
        </div>
        <button class="btn btn-md" ng-click="uploadPicture()">Upload Picture</button>
    </form>
</div>

In the controller:

profile.controller('manageProfile', ['$scope', function($scope) {
    $scope.uploadPicture = function() {
        /* replace this
        var fileUploadControl = $("#profPic")[0];
        var file = fileUploadControl.file[0];
        var name = file.name;
        */
        //Use ng-model variable
        var name = $scope.fileName;
        //
    };

}]);

For more information on ng-model, see AngularJS ng-model Directive API Reference.

Sign up to request clarification or add additional context in comments.

1 Comment

done change the code just like the answer, but still got Error: this._source is undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.