设置内容类型:在 angular js post 请求中

setting Content-Type: in angular js post request

<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);
    });

我想知道如何以及在哪里指定 内容类型:multipart/form-data 在此 angularjs post 请求中?

请协助。 默认值似乎是 "application/json, text/plain," 这不适用于 image/file 上传。

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

上面的代码总是回显不成功

$http.post angular 中的快捷方法需要三个参数,url,请求数据和一个配置 object您可以像下面这样设置 headers :

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

在你的情况下它将是:

$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);
});

您还可以像下面这样构造请求 object:

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

然后像这样提出请求:

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

如果你想设置通用应用程序范围 headers ,你可以使用默认 headers 它将默认添加到所有请求中,如下所示:

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

这将为所有请求添加上述内容类型。

文档中的更多信息here