0

i am doing file uploading using angularjs. I am getting response but can't store that file into local disk.

controller

(function ()
{
'use strict';

angular
    .module('app.message')
    .controller('MessageController', MessageController);

/** @ngInject */
MessageController.$inject = ['$scope', '$http', '$location', 'fileUpload'];
function MessageController($scope, $http, $location, $rootScope, fileUpload)
{
    var vm = this;



    vm.uploadFile = function(){

        var file = $scope.myFile;

        console.log('file is ' );
        console.dir(file);

        var uploadUrl = "./uploads";
        fileUpload.uploadFileToUrl(file, uploadUrl);
    };
}
})();

service

(function ()
{
'use strict';

angular
    .module('app.core')
    .factory('fileUpload', fileUpload);

/** @ngInject */
function fileUpload()
{
    console.log('dsfsdfdsf');

      this.uploadFileToUrl = function(file, uploadUrl){
          console.log('inside');
           var fd = new FormData();
           fd.append('file', file);

           $http.post(uploadUrl, fd, {
              transformRequest: angular.identity,
              headers: {'Content-Type': undefined}
           })

           .success(function(){
           })

           .error(function(){
           });
    }
    return this;
}
}());

view

<div flex style="max-width:700px;">
    <input type = "file" file-model = "myFile"/>
    <button ng-click = "vm.uploadFile()">upload me</button>
</div>

In console i am getting file details but i is not store in the disk. The error is always shows like fileUpload is undefined in controller.

Console error pic

3
  • 1
    i think you should remove injection of your service named 'fileUpload' from MessageController.$inject = ['$scope', '$http', '$location', 'fileUpload']; Only keep it within function MessageController($scope, $http, $location, $rootScope, fileUpload) Commented Mar 10, 2016 at 10:23
  • thank you. I removed 'fileUpload' from MessageController.$inject = ['$scope', '$http', '$location', 'fileUpload']; Now it's working properly. Commented Mar 10, 2016 at 12:05
  • now i'm posting my comment as answer if it help you please accept it or vote up for me...thanks in advance Commented Mar 10, 2016 at 12:09

2 Answers 2

1

You should try this:

(function ()
{
'use strict';

angular
    .module('app.message')
    .controller('MessageController', MessageController);

/** @ngInject */
MessageController.$inject = ['$scope', '$http', '$location', '$rootScope'];
function MessageController($scope, $http, $location, $rootScope, fileUpload)
{
    var vm = this;



    vm.uploadFile = function(){

        var file = $scope.myFile;

        console.log('file is ' );
        console.dir(file);

        var uploadUrl = "./uploads";
        fileUpload.uploadFileToUrl(file, uploadUrl);
    };
}
})();
Sign up to request clarification or add additional context in comments.

3 Comments

after request to sent to node file it shows error like Unexpected Field. What should i do now? Help me
i am getting error like { [Error: Unexpected field] code: 'LIMIT_UNEXPECTED_FILE', field: 'file', storageErrors: [] } . I solved that problem, thanks for your concern.
i think you are uploading multiple files but receiving in single field in your app.js file...refer this link stackoverflow.com/questions/34798593/…
0

you forget $rootScope injection

/** @ngInject */
MessageController.$inject = ['$scope', '$http', '$location','$rootScope', 'fileUpload'];
function MessageController($scope, $http, $location, $rootScope, fileUpload)
{
    var vm = this;



    vm.uploadFile = function(){

        var file = $scope.myFile;

        console.log('file is ' );
        console.dir(file);

        var uploadUrl = "./uploads";
        fileUpload.uploadFileToUrl(file, uploadUrl);
    };
}
})();

Comments

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.