由于很多天我试图在服务器中上传文件,所以在实时服务器中出现405种方法不允许错误: 这是我的看法: HTML和javascript
@extends('layouts.app')
@section('content')
form id="uploaddiamond" class="form-horizontal form-label-left" method="post" enctype="multipart/form-data">
@csrf
<div class="col-md-6">
<div class="block">
<div class="panel-body">
<div class="form-group">
<label class="col-md-3 control-label">Upload Diamond <span class="required">*</span></label>
<div class="col-md-9">
<input required="" type="file" name="result_file" id="result_file" />
</div>
</div>
<div class="btn-group pull-right">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</div>
</div>
</div>
</form>
@endsection()
@section('javascript')
<script>
$("#uploaddiamond").on("submit",function(e) {
e.preventDefault();
console.log('tst');
$.ajaxSetup({
headers: {
'X-CSRF-Token': $('meta[name=_token]').attr('content')
}
});
var file_data = $('#result_file').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: "{{ route('diamond') }}", // point to server-side PHP script
data: form_data,
type: 'POST',
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData: false,
success: function(data) {
console.log(data);
}
});
});
</script>
@endsection()
This is my web route:
Route::get('/imageview','ImageController@index')->name('getimage');
Route::post('/postDiamond','ImageController@postDiamond')->name('diamond');
This is my controller:
public function index(){
return view('Image/imgupload');
}
public function postDiamond(Request $request){
dd($request->file('file'));
$supplier_name = $request->supplier_name;
$extension = $request->file('file');
$extension = $request->file('file')->getClientOriginalExtension(); // getting excel extension
$dir = 'assets/files/';
$filename = uniqid().'_'.time().'_'.date('Ymd').'.'.$extension;
$request->file('file')->move($dir, $filename);
}
我不为什么该代码不起作用,因为该代码在localhost中有效,但在Linux托管中不起作用: 有人可以帮我做一些版本错误吗 服务器当前PHP版本:7.3.17 laravel PHP版本:7.1.10
这是我的服务器错误图片,请检查: enter image description here
答案 0 :(得分:0)
好吧,我刚刚在实时服务器上测试了此代码,并且工作正常。如果此代码仍然无法为您服务,则需要检查服务器端某些文件的权限,例如
web.php
等...
$(document).on('click','#submit_button', function(e){ //#submit_button id on submit button
e.preventDefault();
var formData = new FormData(this.form);
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
}
});
$.ajax({
method: 'POST',
url: '{{ route("diamond") }}',
cache: false,
contentType: false,
processData: false,
data: formData,
success: function(data){
console.log(data);
},
error: function(data){
console.log(data);
}
});
});
请确保从<form>
标记中删除@csrf。
只需在控制器dd(request()->all());
中查看您得到的内容