Laravel使用AngularJS从多个文件上传文件中仅存储一个文件 [英] Laravel storing only one file from multiple file upload file using AngularJS

查看:108
本文介绍了Laravel使用AngularJS从多个文件上传文件中仅存储一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好或晚上好(取决于您阅读本主题的时间).

Good morning or good evening (it depends whenever you read this topic).

我使用angular和laravel编写了此代码,以上传多个文件, 但是我从请求中得到的只是一个文件对象,而所有其他文件对象都被释放了,因此在laravel控制器中,它不会在foreach中循环并返回未定义.

I wrote this code using angular and laravel to upload multiple files, but what i get from the request is only one file object, and all others are discharged, for this reason in laravel controller doesn't loop in foreach and return undefined.

请给我反馈

HTML

<input ng-file-model = "files" value="Seleziona file" type="file"
       name = "file[]" class="btn btn-info btn-s" multiple />

控制器(AngularJS)

Controller (AngularJS)

$scope.upload = function(){
    var obj = {};
    obj.pratica = $scope.pra.id;
    obj.cliente = $scope.cliente.id;
    obj.tipoDoc = $scope.getTipoDoc;
    obj.file = $scope.files;
    dati.uploadFile(obj)
        .success(function(result){
            $rootScope.stampaGritter(result.message, "success");
        })
        .error(function(result){
            $rootScope.stampaGritter(result.message, "error");
        });
};

app.directive('ngFileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            var model = $parse(attrs.ngFileModel);
            var isMultiple = attrs.multiple;
            var modelSetter = model.assign;
            element.bind('change', function () {
                angular.forEach(element[0].files, function (item) {
                    scope.files.push(item);
                });
                scope.$apply(function () {
                    if (isMultiple) {
                        modelSetter(scope, scope.files);
                    } else {
                        modelSetter(scope, scope.files[0]);
                    }
                });
            });
        }
    };
}]);

uploadFile: function(obj) {
    var fd = new FormData();
    angular.forEach(obj.file, function(item) {
        fd.append('file', item);
    });
    fd.append('tipo_doc', obj.tipoDoc);
    fd.append('cliente', obj.cliente);
    fd.append('pratica', obj.pratica);
    $http.post(
        "api/storeFile",
        fd,
        {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined
        }
    })
},

控制器(Laravel)

Controller (Laravel)

public function storeFile(Request $request) {
    /*$file = $request->file('file');
    $filename = $file->getClientOriginalName();
    $guid = $this->getGUID();
    $file->move(storage_path()."\\app\\file", $guid);
    $response = [
        "file" => $filename,
        "GUID" => $guid
    ];
    $file = new Documenti($response);
    $file->save();*/
    $cli = $request->input('cliente');
    $pra = $request->input('pratica');
    $tipoDoc = $request->input('tipo_doc');
    $files = $request->file('file');
    dd($files);
    foreach($files as $file) {
        $guid = $this->getGUID();
        $file->move(storage_path()."\\app\\file", $guid);
        $response = [
            'cliente_id' => $cli,
            'pratica_id' => $pra,
            'tipo_id' => $tipoDoc,
            'file' => $file->getClientOriginalName(),
            'GUID' => $guid
        ];
        $file = new Documenti($response);
        $file->save();
    }
    return response()->json($response);
}

当dd($ files)打印时

When dd($files) it prints

请求有效载荷:

------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="file"; filename="autorizzazioni2.CSV"
Content-Type: application/octet-stream


------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="file"; filename="bustapaga.pdf"
Content-Type: application/pdf


------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="tipo_doc"

7
------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="cliente"

2
------WebKitFormBoundary1yzFmBAy2rTa73eV
Content-Disposition: form-data; name="pratica"

2
------WebKitFormBoundary1yzFmBAy2rTa73eV--

推荐答案

Laravel具有store函数使用此

Laravel has store function use this

foreach ($request->file as $file) {
  $filename = $file->store('folder');
}

此处folderstorage/app/folder

您需要像这样在FormData中进行更改,您必须在FormData

You need to change in FormData like this, you have to add file[] array in FormData

for (let i = 0; i < obj.file.length; i++) {
     let f = obj.file[i];
     fd.append('file[]', f);
 }

这篇关于Laravel使用AngularJS从多个文件上传文件中仅存储一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆