0

I am getting an issue with the file upload from angular to laravel 5.2. Throws an error of "Call to a member function getClientOriginalExtension() on string"

Routes:

Route::get('fileupload', array('as' => 'fileupload.index', 'uses'=>'FileController@index'));
Route::post('filehandler/submit', array('as' => 'filehandler.submit', 'uses' => 'FileController@store'));

View:

<!DOCTYPE html>
<html ng-app="myApp">
<div  ng-controller="fileCtrl">
<form ng-submit="addList1()" enctype="multipart/form-data">
  <input type="hidden" name="_token" value="{{ csrf_token() }}">
  <input type="text" name="text" ng-model="info.text">
  <input type="file" name="file" ng-model="info.file" id="uploads" required>
  <button type="submit">Submit</button>
</form>
<script type="text/javascript" src="{{URL::to('/')}}/angular/bower_components/jquery/dist/jquery.js"></script>
<script type="text/javascript" src="{{URL::to('/')}}/angular/bower_components/angular/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-animate.js"></script>    
<script type="text/javascript" src="{{URL::to('/')}}/angular/bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
<script type="text/javascript" src="{{URL::to('/')}}/angular/js/all.js"></script>

Controller:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use View;
use File;
use DB;

class FileController extends Controller
{
public function index()
{
    return View('file');
}
public function store(Request $request)
{
$input = Input::all();

    $destinationPath = 'uploads'; // upload path
    $file = $input['file'];
    $extension = $file->getClientOriginalExtension();
    $fileName = rand(11111,99999).'.'.$extension;
    $file= Input::file('file')->move($destinationPath, $fileName);
    print_r($file);    
    exit;
}
}

My JS file(all.js)

angular.module('myApp',[])
myApp.service('data', function ($http) {
var service ={};
service.postFile = function(info){
    var promise = $http.post('filehandler/submit', info);

    promise.then(function(response){
        angular.extend(info, response.data);
    });

    return promise;
};
return service;
})
.controller('fileCtrl', function($scope, data) {
$scope.info = {
    "text": "",
    "file": []
};
$scope.addList1 = function(form){            
    data.postFile($scope.info).then(function(response){
    console.log("hello", $scope.info);
    });             
};
})

It is working fine without angular, when I am implement through angular it shows error in my controller. In FileController I am getting an error "Call to a member function getClientOriginalExtension() on string". Can anyone please help me it to fix this out, Thanks in advance

3
  • What does "dd($input['file']);" return? Commented Jun 6, 2016 at 11:07
  • @jedrzej.kurylo It returns my uploaded file name "bg_piece.jpg" Commented Jun 6, 2016 at 11:17
  • I guess you need to create a directive. This might be helpful: stackoverflow.com/questions/33534497/…. You still need to adapt to laravel. Commented Jun 6, 2016 at 13:31

1 Answer 1

0

In order to get the uploaded file you need to call Request's file() method.

Replace

$file = $input['file'];

with

$file = $request->file('file');
Sign up to request clarification or add additional context in comments.

1 Comment

It returns null value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.