gpt4 book ai didi

javascript - 拉拉维尔 5.8 : Why $request->hasFile returns false

转载 作者:行者123 更新时间:2023-12-02 22:43:48 25 4
gpt4 key购买 nike

我将一个文件从 HTML 发送到我的 Controller ,并在 Controller 中 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>
这是我的 Controller :

   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.') }

感谢您的提前^-^

最佳答案

来自文件输入的数据是 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();
}
}
})
});

关于javascript - 拉拉维尔 5.8 : Why $request->hasFile returns false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58493237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com