gpt4 book ai didi

javascript - 在 AngularJS 中发送图像/jpeg 以及其他表单数据

转载 作者:行者123 更新时间:2023-12-02 16:59:45 25 4
gpt4 key购买 nike

在尝试研究如何使用 AngularJS 将图像和其他表单数据提交到 REST API 的完整方法之后。该图像始终在服务器上被截获为“null”,尽管在使用 postman chrome 测试 REST API 时它工作得很好。扩展名。

下面是 Controller 代码:

$scope.submitForm = function(){
var formData=new FormData();
formData.append('image', $scope.fileName);
formData.append('title', $scope.subject);
formData.append('body', $scope.body);
$http({
method : 'POST',
url : 'formhandler',
data : formData,
headers : {'Content-Type': undefined},
transformRequest: function(data, headersGetterFunction) {
return data;
}
}).success(function(data) {
$scope.openModal.hide();
});
};

预先感谢您,非常感谢您的帮助。

最佳答案

经过长时间的调查,问题似乎出在文件指令中,而不是在 Controller 中,通过使用下面的指令代码,我已经成功地将图像文件发布到服务器。

angular.module('yourApp').directive('file', [
function() {
return {
restrict: "E",
template: "<input type=\"file\" />",
replace: true,
require: "ngModel",
link: function(scope, element, attr, ctrl) {
var listener;
listener = function() {
return scope.$apply(function() {
if (attr.multiple) {
return ctrl.$setViewValue(element[0].files);
} else {
return ctrl.$setViewValue(element[0].files[0]);
}
});
};
return element.bind("change", listener);
}
};
}
]);

了解更多信息;检查以下链接: https://gist.github.com/tdrozdowski/6468541

关于javascript - 在 AngularJS 中发送图像/jpeg 以及其他表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25847467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com