I've watched and read a bunch of videos and tutorials but I'm getting a 403 error.
I'm using Angular 1.
First, I made an Angular directive because ng-model doesn't work with files. I called my directive file-model:
app.directive('fileModel',['$parse', function ($parse){
return {
restrict: 'A',
link: function (scope, element, attrs) {
element.bind('change', function () {
$parse(attrs.fileModel)
.assign(scope, element[0].files[0])
scope.$apply();
})
}
}
}]);
Then I used my directive in the HTML template:
<form ng-submit="uploadFile(file)">
<input type="file" accept="txt" file-model="file" class="form-control">
<button type="submit" class="btn btn-primary">Upload File</button>
</form>
And I made a handler in the controller:
app.controller('myController', ['$scope', '$firebaseStorage', function($scope, $firebaseStorage) {
// Create a Firebase Storage reference
var storage = firebase.storage();
var storageRef = storage.ref();
var filesRef = storageRef.child('files');
$scope.uploadFile = function(file) {
console.log("Let's upload a file!");
console.log($scope.file);
var storageRef = firebase.storage().ref("files");
$firebaseStorage(filesRef).$put($scope.file);
};
}]);
Lastly I set the Firebase Storage rules to "public":
service firebase.storage {
match /b/myFirebaseProject.appspot.com/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
Running all this, I can select my test file, click Upload File, and the file loads into the $scope. My directive is working. But then I get an error message:
POST https://firebasestorage.googleapis.com/v0/b/myFirebaseProject.appspot.com/o?name=files 403 ()
I tried creating a files folder in Firebase Storage but I got the same 403 error.
Thanks!