我正在尝试上传laravel中的文件。但每次点击提交按钮时,它都会在控制台中显示内部服务器错误。我已经通过get请求检查了死记硬背,以检查控制器功能是否正常工作并且工作正常。可以说有什么问题吗? 这是我的代码示例
路线代码
Route::post('/storefile','PublicationController@storeFile');
控制器
public function storeFile(Request $request){
if($request->ajax()){
echo "got";
}
else echo "not ajax";
}
视图
@extends('layouts.app')
@section('stylesheet')
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link type="text/css" href="/css/bootstrap-tagging.css" rel="stylesheet">
@endsection
@section('content')
<div class="validation-system">
<div class="validation-form">
<form id="test-form" action="/storepublication" method="post" enctype="multipart/form-data" >
{!! csrf_field() !!}
<div class="col-md-3 form-group1">
<label class="control-label">Upload Paper</label>
<input type="file" name="file" id="paper">
</div>
<div class="col-md-3 form-group1">
<input type="submit" id="submit" name="submit" class="btn btn-primary" value="Add">
</div>
</form>
</div>
</div>
@endsection
@section('scripts')
<script>
$(document).ready(function() {
$("#test-form").submit(function (event) {
event.preventDefault();
var file_data = $('#paper').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: "/storefile",
type: "post",
data: form_data,
processData: false,
contentType: false,
success: function (res) {
document.getElementById("response").innerHTML = res;
}
});
});
});
</script>
@endsection
答案 0 :(得分:1)
尝试替换此
var file_data = $('#paper').prop('files')[0];
由此
var file_data = $('#paper').files[0];