Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
44
rated 0 times [  51] [ 7]  / answers: 1 / hits: 19844  / 8 Years ago, tue, november 15, 2016, 12:00:00
<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.


More From » angularjs

 Answers
7

$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


[#60055] Saturday, November 12, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bryonk

Total Points: 161
Total Questions: 116
Total Answers: 107

Location: Albania
Member since Sun, Nov 22, 2020
4 Years ago
bryonk questions
;