我从HTML向控制器发送了一个文件,并在控制器中var_dump我的文件,但是返回结果为false。
var_dump($request->hasFile('logo')
如果我使用HTML执行它,结果是否正确?如果我使用Ajax,则结果为假。这是一个简单的文件输入:
<section class="section bg-img" id="section-contact" style="background-image: url({{ asset('front/img/bg-cup.jpg') }})" data-overlay="8">
<div class="container">
<div class="row gap-y">
<div class="col-12 col-md-6 offset-md-3 form-section">
{!! Form::open(['id'=>'confirmation','class'=>'row', 'method'=>'POST', 'enctype' => 'multipart/form-data']) !!}
<div class="col-12 col-md-10 bg-white px-30 py-45 rounded">
<p id="alert"></p>
<div id="form-box">
<div>
<span class="btn btn-info btn-file">
<input type="file" name="logo" id="logo"> </span>
</div>
<div class="col-md-12">
<button class="btn btn-lg btn-block btn-primary" type="button" id="save-form">SignUp</button>
</div>
</div>
</div>
{{!! Form::close() !!}
/div>
</div>
</div>
</section>
这是我的Ajax
<script>
$('#save-form').click(function () {
$.easyAjax({
url: '{{route('front.confirmation',$confirm->billing_number)}}',
container: '.form-section',
enctype: 'multipart/form-data',
type: "POST",
data: $('#confirmation').serialize(),
messagePosition: "inline",
success: function (response) {
if(response.status == 'success'){
$('#form-box').remove();
}
}
})});</script>
public function update(Request $request, $billing){
$confirm = PaymentConfirmation::where('billing_number',$billing)->first();
var_dump($request->hasFile('logo')); //Here is return false
if ($request->hasFile('logo')) {
$confirm->invoice =$request->logo->hashName();
$request->logo->store('user-uploads/invoice');
}
$confirm->save();
return Reply::success('Thank you for upload.') }
感谢提前^-^
答案 0 :(得分:1)
来自文件输入的数据为not serialized by the jquery .serialize() method,,因此该文件确实没有被发送回服务器
要上传文件,请改用FormData()
$('#save-form').click(function() {
$.easyAjax({
url: '{{route('front.confirmation ',$confirm->billing_number)}}',
container: '.form-section',
enctype: 'multipart/form-data',
type: "POST",
data: new FormData($('#confirmation')),
messagePosition: "inline",
success: function(response) {
if (response.status == 'success') {
$('#form-box').remove();
}
}
})
});