1
<input type="file" ng-model="articleimg" placeholder="upload related img">    

$http.post($scope.base_url+'create.php', 
            {params {view:'view',articleimg:$scope.articleimg}})
            .then(function(response){
                    console.log(response);
    });

I would like to know how and where to specify the content-type: multipart/form-data in this angularjs post request?

Please assist. the default seems to be "application/json, text/plain," which does not work with the image/file uploads.

if(isset($_FILES['articleimg'])===true ){
  echo "sucessfull";
}else echo "unsuccessfull";

the code above alway echos unsuccessfull.

1 Answer 1

6

$http.post shortcut method in angular takes three parameters, url, request data and a config object in which you can set headers like below :

$http.post('/someUrl', data, {headers:{'Content-Type': 'multipart/form-data'}}).then(successCallback, errorCallback);

In your case it will be :

$http.post($scope.base_url+'create.php', 
        {params {view:'view',articleimg:$scope.articleimg}}, {headers:{'Content-Type': 'multipart/form-data'})
        .then(function(response){
                console.log(response);
});

You can also construct the request object like below :

{
 method: 'POST',
 url: $scope.base_url+'create.php',
 headers: {
   'Content-Type': 'multipart/form-data'
 },
 data: {params {view:'view',articleimg:$scope.articleimg}}
}

and then make the request like this :

$http(req).then(function(){...}, function(){...});

If you want to set common application wide headers , you can use default headers which will be added to all the requests by default like below :

$httpProvider.defaults.headers.common['Content-Type'] = 'multipart/form-data';

This will add the above mentioned content type for all the requests.

More information in the documentation here

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

3 Comments

the "-" in the Content-Type is throwing and error. unexpected token.
Tried the second option " $http(req)" as well but it is just not showing up on the php server. when i try this if(isset($_FILES['articleimg'])===true ){ echo "sucessfull"; }else echo "unsuccessfull";
adding the quotes did remove the error. thanks. but no lock on the sever side.

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.