[英]Angular JS File Upload and Laravel Backend
我目前在使用文件上传功能以及 angular 中的其他字段时遇到问题。 我尝试测试我的 Laravel 函数,以使用 POSTMAN 保存和上传其他字段的文件,并且它工作正常。
但是,在尝试使用 Angular JS Form 时,它给了我这样的错误:
这是我的 angular js 代码,用于从表单获取所有输入并将其传递给 laravel:
$scope.addAccount = function(obj) {
if ($scope.transformForm.$valid && $scope.netForm.$valid) {
$scope.uploadAccountProf();
//$state.go("detailed");
DataFactory.AddAccount($scope.obj).then(function successCallback(response) {
console.log("success");
Materialize.toast('Account Saved!', 8000, 'greenbg rounded nobr');
$state.go('detailed');
}, function errorCallback(response) {
console.log("Error");
});
}
else{
console.log("try again");
Materialize.toast('Please fill out all required field.', 8000, 'redbg rounded nobr');
}
下面的代码是负责上传文件的东西:
$scope.uploadAccountProf = function(roastSlidesUpload){
var fileAccount = angular.element(document.querySelector('#accountProfile'));
var fileCompliance = angular.element(document.querySelector('#fileUpload'));
//console.log(myRoastSlide[0].files[0]);
$scope.obj['accountProfile']=fileAccount[0].files[0];
$scope.obj['fileUpload']=fileCompliance[0].files[0];
console.log($scope.obj);
var formData = new FormData();
formData.append('file', $scope.obj);
console.log("asd",formData);
return $scope.obj;
};
我的表格:
<form id="transformForm" name="transformForm" class="addingTransformation" novalidate>
以下是我仅用于文件上传的表单,不包括其他文本框:
<div class="row">
<div class="file-field input-field col s12">
<div class="btn">
<span>File</span>
<input id="accountProfile" type="file">
</div>
<div class="file-path-wrapper">
<input id="acctProfile" class="file-path validate" placeholder="Accout Profile" type="text">
</div>
</div>
</div>
我的按钮:
<button class="btn waves-effect waves-light right submit allmargin" type="submit" name="action" ng-click="verifiedaccount()">Submit
<i class="material-icons right">send</i>
</button>
有人能帮我吗? 几个星期以来,我一直试图解决这个问题,但仍然无法弄清楚出了什么问题,以及为什么 angular 会将该错误返回给我。 :(
我对Angular的经验很少,所以我不知道它是否正确,但是通过HTML表单上传文件,您通常需要设置它的enctype。
您可以将enctype="multipart/form-data"
到您的<form>
标签并重试吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.