Sharath
Sharath

Reputation: 2428

Upload multiple files in angularjs and backend using laravel (without form tag)

I want to upload multiple files without using form tag and using angular js and i am using laravel 5.2. Below code i tried sending only 1 file it works but on multiple files it fails.

HTML Code

<input type="file" class="upload_file pull-right" multiple />

JQuery Code

$(".upload_file").change(function ()
{
    if (this.files && this.files.length > 0)
    {
         scope.uploadFile(this.files);
    }
});

AngularJs Code

scope = $scope;

$scope.uploadFile = function(files)
{
    var fd = new FormData();
    fd.append("file", files);
    $http.post(API_URL + '/offline_upload/file/?token=' + token,fd,
    {
        withCredentials: true,
        headers: {'Content-Type': undefined },
        transformRequest: angular.identity
    })
    .success(function(data)
    {
        alert('Done');
    });
 };

Laravel PHP Code

$files = $request->file('file');
Log::info(count($files));

Here the count is always 0

In angularjs instead of files if i send files[0] then only 1 file is sent. But how to send multiple files and retrieve it.

Can someone please help me solve this issue.

Upvotes: 1

Views: 597

Answers (1)

cssBlaster21895
cssBlaster21895

Reputation: 3710

You can try that with https://github.com/nervgh/angular-file-upload, it supports input type file with multiple parameter and you don't need jquery.

You just instate new uploader object: $scope.uploader = new FileUploader({var options;}); and put directive into upload form <input type="file" nv-file-select uploader="uploader"/> inside your html.

Upvotes: 1

Related Questions