簡體   English   中英

Angular JS 文件上傳和 Laravel 后端

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM