【发布时间】:2016-06-06 10:57:20
【问题描述】:
我遇到了从 angular 到 laravel 5.2 的文件上传问题。抛出“Call to a member function 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 中,我收到错误消息“Call to a member function getClientOriginalExtension() on string”。谁能帮我解决这个问题,在此先感谢
【问题讨论】:
-
什么是“dd($input['file']);”返回?
-
@jedrzej.kurylo 它返回我上传的文件名“bg_piece.jpg”
-
我猜你需要创建一个指令。这可能会有所帮助:stackoverflow.com/questions/33534497/…。你仍然需要适应 laravel。
标签: php angularjs laravel laravel-5 laravel-5.2