到目前为止,我已经创建了一个带有链接和控制器的指令,该指令在插入输入文件时发送,HTTP发送到nodeJS中的API。问题出在指令中:
一般来说,我想知道:我做得对吗?
在控制台日志中打印出来的错误:
TypeError:$ http(...)。success不是函数 [Więcejinformacji]
和
可能未处理的拒绝:{“data”:“Multipart:Boundary not found错误:Multipart:Boundary not found \ n at new
HTML:
<input type="file" file="file" required />
指令:
App.directive('file', function () {
return {
scope: {
file: '='
},
link: function (scope, el, attrs) {
el.bind('change', function (event) {
var file = event.target.files[0];
scope.file = file ? file : undefined;
scope.$apply();
scope.check();
});
},
controller: function($scope, $http) {
$scope.check = () => {
if($scope.file) {
$http({
method: 'POST',
url: '/card/upload-image',
headers: {
'Content-Type': 'multipart/form-data'
},
data: {
upload: $scope.file
},
transformRequest: function (data, headersGetter) {
var formData = new FormData();
angular.forEach(data, function (value, key) {
formData.append(key, value);
});
var headers = headersGetter();
delete headers['Content-Type'];
return formData;
}
})
.success(function (data) {
console.log(data)
})
.error(function (data, status) {
console.log(data, status)
});
}
}
}
};
});