1

I am trying to post the image informations to back-end. I tried with $resource but not working. But when i tried with $http it seems that works. But i have added some of additiona params to $http - but i don't know how i can apply those params to $resource any one help me please?

$scope.photoData = function (photoData) {

         console.log( "photo data", photoData ); //consoles properly

        var fileData = photoData[0];
        var data = new FormData();

         var fileASHXPath =   _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/UploadImage/';

         //this is not working!
         $resource(fileASHXPath).save({ "photoData" : fileData }).$promise.then( function ( response ) {

            console.log( response ); //nothing works!

         })

        //this is works!

         var base = _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/UploadImage/';

         $http({
             url: base + 'TestImage.jpg',
             method: 'POST',
             processData: false,
             headers: { 'Content-Type': undefined },
             data: fileData
         }
                 )
                 .success(function (data) {

                     if (data.Message.toLowerCase() == 'success') {

                         console.log(data);
                     }                    

                 })
                 .error(function (msg, code) {

                     console.log('error method called');

                 });

     }

UPDATE - My NEW TRY

Still not working

$resource(base + fileName, {}, {

            post:{
                method:"POST",
                isArray:false,
                headers:{'Content-Type':'undefined'}
            }

        }).$promise.then(function ( response ) {

            console.log( "response is", response );

        })
4
  • you might have forgot to declare it as a parameter for controller function. Commented Jan 5, 2016 at 5:04
  • I am not getting you.. please help me further, If possible post me your suggesion Commented Jan 5, 2016 at 5:08
  • As far I know, we will get $scope, $http and others by declaring as a parameters right? In my application I was getting $window as undefined because I wasn't using that parameter in controller declared function. (app.controller("ctrlLogin", function ($scope, $http, $location, $window) {});). So I think you may have forgot to declare as a parameter. Commented Jan 5, 2016 at 5:16
  • No, I have declared those. generally in case if I am not declare, I will get error. In my case I din't get any error at all. Commented Jan 5, 2016 at 5:17

2 Answers 2

1

try this. That is after $resource definition, call post() with form data.

$scope.photoData = function (photoData) {

  console.log( "photo data", photoData ); //consoles properly

  var fd = new FormData();
  fd.append("photoData", photoData[0]);

  var fileASHXPath =   _spPageContextInfo.webAbsoluteUrl + '/_vti_bin/CPMD.WEBSERVICE/ProjectInfoService.svc/UploadImage/';

  $resource(fileASHXPath, {}, {
      post: {
        method: 'POST',
        transformRequest: angular.identity,
        headers: { 'Content-Type': undefined }
      }
    }
    ).post({}, fd).$promise.then(function ( response ) {

      console.log( "response is", response );

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

2 Comments

from fd how can i get the 'image name` ? ( usually i do photoData[0].name
You can't get image name because FormData doesn't provide it. It is just an object. You can get name from any backend language like PHP etc..
0

Try invoking the resource. That is after $resource definition, call post().

    $resource(base + fileName, {}, {

        post:{
            method:"POST",
            isArray:false,
            headers:{'Content-Type':'undefined'}
        }

    }).post().$promise.then(function ( response ) {

        console.log( "response is", response );

    })

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.