我遇到了从angular到laravel 5.2的文件上传问题。引发错误" 调用成员函数getClientOriginalExtension()on string "
路线:
Route::get('fileupload', array('as' => 'fileupload.index', 'uses'=>'FileController@index'));
Route::post('filehandler/submit', array('as' => 'filehandler.submit', 'uses' => 'FileController@store'));
查看:
<!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>
控制器:
<?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;
}
}
我的JS文件(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);
});
};
})
没有角度工作正常,当我通过角度实现它在我的控制器中显示错误。在 FileController 中我收到错误&#34; 在字符串&#34;上调用成员函数getClientOriginalExtension()。任何人都可以帮我解决这个问题,提前谢谢
答案 0 :(得分:0)
要获取上传的文件,您需要调用请求文件()方法。
替换
$file = $input['file'];
与
$file = $request->file('file');