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
In order to get the uploaded file you need to call Request's file() method.
Replace
$file = $input['file'];
with
$file = $request->file('file');