Laravel 5.5 - $request->file 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47188656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:55:52  来源:igfitidea点击:

Laravel 5.5 - $request->file returns null

phpformslaravel

提问by Taps101

I have created a form that uploads a file but it returns a null value when I submit. When I add in the enctype="multipart/form-data" it reloads the page and doesn't seem to go through my controller.

我创建了一个上传文件的表单,但在我提交时它返回一个空值。当我添加 enctype="multipart/form-data" 时,它会重新加载页面并且似乎没有通过我的控制器。

MY HTML FORM

我的 HTML 表单

<form class="form-horizontal" role="form" name="importform" method="POST" action="{{ route('import_type') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="control-group">
  <label class="control-label">&nbsp;</label>
<div class="controls">
<div class="control-group text-center">
  <label class="btn btn-primary" for="file-selector">
  <input id="file-selector" name="template_upload" type="file" value="" required autofocus style="display:none" onchange="$('#upload-file-info').html(this.files[0].name)" required>                                    Upload List               </label>
   <span class='label label-default' id="upload-file-info"></span>
   </div>
   </div>
   </div>
   <div class="control-group">
   <div class="controls">
    <input class="btn btn-primary" type="submit" id="import-submit" name="import-submit">
    </div>
    </div>
    </form>

MY CONTROLLER: I am using the import method

我的控制器:我正在使用导入方法

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ImportTypeRequest;
use \App\Guest;
use \App\Role;
use \App\User;
use \App\Type;
use Illuminate\Support\Facades\Auth;



class GuestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $user = User::with('roles')->where('id', Auth::id())->get();
        $types = Type::where('user_id', Auth::id())
            ->where('active',1)->get();


        return view('view_import',compact('user','types'));

    }

    public function import(ImportTypeRequest $request)
    {


        $template_upload = $request->file('template_upload');
        dd($template_upload);
    }
}

采纳答案by Ahmed Essam

Here are some suggested ways trying to solve this.

以下是一些尝试解决此问题的建议方法。

First of all in your import method add dd($request->all())at its top and see what's the response. You should see all your form data and of course template_uploadfile. That's how you make sure that you see all the coming data from your form to your controller method.

首先,在您的导入方法中dd($request->all())在其顶部添加并查看响应。您应该看到所有表单数据,当然还有template_upload文件。这就是您如何确保看到从表单到控制器方法的所有即将到来的数据。

Then try to get rid of ImportTypeRequestand just use Illuminate\Http\Requestto see what will you get. If you got different result then the problem is in ImportTypeRequestclass.

然后尝试摆脱ImportTypeRequest并使用Illuminate\Http\Request,看看你会得到什么。如果你得到不同的结果,那么问题出在ImportTypeRequest课堂上。

Then why don't you just use $request->template_upload?! It's cleaner I guess.

那你为什么不直接使用$request->template_upload?!我想它更干净。