[英]Angular File upload formData
我正在使用Angular-File-Upload ,問題是我無法將formData與文件一起發送。 以下是我的代碼:
$scope.addProducts = function(){
console.log($scope.product);
ProductApi.addProduct($scope.product)
.then(function(response){
ngNotify.set('Your product has been added!', {
position: 'bottom',
duration: 2000
});
$scope.product_id = response.data;
uploaderImages.uploadAll();
})
.catch(function(response){
console.log(response.data);
})
}
上面的代碼所做的是,一旦提交表單,該表單將通過api調用發送。 響應將是product_id
和uploaderImages.uploadAll();
product_id
被觸發! 以下是將文件發布到服務器的uploaderImages
:
var uploaderImages = $scope.uploaderImages = new FileUploader({
url: '/api/productimg',
onBeforeUploadItem: function(prod){
var ids = $scope.product_id
var prodid = { proid: $scope.product_id} ---> empty
prod.formData.push(prodid)
console.log($scope.product_id) ----> product_id = 32
},
onCompleteAll: function(){
console.log($scope.product_id); ----> product_id = 32
},
onSuccessItem: function(prodId,response,status,headers){
}
});
我不知道如何解決這個prob: proid:product_id
返回為[object Object]
,如果我將proid分配為一個固定的整數,即proid:23
,它就可以工作。
請幫忙!!!!!
您不僅需要訪問有效負載,還需要訪問product_id的值:
$scope.product_id = response.data.product_id;
另外,由於您正在使用Promises,因此需要鏈接方法。 嘗試:
$scope.addProducts = function(){
console.log($scope.product);
ProductApi.addProduct($scope.product)
.then(function(response) {
return ngNotify.set('Your product has been added!', {
position: 'bottom',
duration: 2000
}, function(error) {
console.log(error);
}) // assuming this is async so add another then block below to execute once this is done
.then(function(response) {
$scope.product_id = response.data.product_id; // response.data will give you the whole response payload if the object returned is {product_id: 123}
uploaderImages.uploadAll();
});
}
請注意,錯誤處理程序是對第一個.then
塊的回調( 請參閱docs )。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.