[英]Pass 2 parameters in $http POST
我有一个方案,一个属性可以有多个图像,但是该图像只能分配给一个属性(一对多,propertyId为FK)。
在html中,我正在检索propertyId并将其与uploadFile函数一起传递
<div id="imageDiv" ng-controller="uploadImageCtrl">
<input type="button" class="btn btn-info" value="Upload Image" ng-click="uploadFile(pro.id)"/>
<input type="file" file-input="files"/>
</div>
在这里,我有我的app.js在其中检索图像,并且propertyId为ID。 我正在尝试传递id和form_data。
app.directive("fileInput", function($parse){
return{
link: function($scope, element, attrs){
element.on("change", function(event){
var files = event.target.files;
console.log(files[0].name);
$parse(attrs.fileInput).assign($scope, element[0].files);
$scope.$apply();
});
}
}
});
app.controller("uploadImageCtrl", function($scope, $http,$routeParams,$location){
$scope.uploadFile = function(id){
var form_data = new FormData();
angular.forEach($scope.files, function(file){
form_data.append('file', file);
});
$scope.id = id;
console.log(id);
$routeParams.id = id;
//$scope.activePath = null;
$scope.updateMessage="";
console.log(form_data);
$http.post('php/uploadImage.php/',{'id':id},form_data,
{
transformRequest: angular.identity,
headers: {'Content-Type': undefined,'Process-Data': false}
}).success(function(response){
alert(response);
});
}
});
然后在uploadImage.php中,我将$ id分配给从app.j检索到的propertyId并将图像上传到该propertyId。
<?php
$data = json_decode(file_get_contents("php://input"));
$id=$data->id;
echo $id;
require_once("connection.php");
$conn = connectToDb();
if(!empty($_FILES))
{
$path = '../Images/' . $_FILES['file']['name'];
if(move_uploaded_file($_FILES['file']['tmp_name'], $path))
{
$insertQuery = "INSERT INTO tbl_images(imagePath , propertyId) VALUES ('".$_FILES['file']['name']."',$id)";
if(mysqli_query($conn, $insertQuery))
{
echo 'File Uploaded';
}
else
{
echo 'File Uploaded But not Saved';
}
}
}
else
{
echo mysqli_error($conn);
}
?>
问题是它给我一个数字8的空白错误,而不是为什么它不上传,8代表$ id(echo $ id).so propertyId被成功检索并传递到php中。
该错误可能是最后一个ELSE语句(回显mysqli_error($ conn))。
是否无法成功传递form_data?
我只需要在id后面附加表单数据,然后使用普通的POST检索id。
app.controller("uploadImageCtrl", function($scope, $http,$routeParams,$location){
$scope.uploadFile = function(id){
var form_data = new FormData();
angular.forEach($scope.files, function(file){
form_data.append('file', file);
});
form_data.append('elmID', id);
$http.post('php/uploadImage.php',form_data,
{
transformRequest: angular.identity,
headers: {
'Content-Type': undefined,
'Process-Data': false
}
}).success(function(response){
alert(response);
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.