laravel 5.4图像上传没有检测到文件

时间:2017-01-30 17:52:36

标签: laravel request laravel-5.3 laravel-5.4

这是我的代码。每次都不返回任何文件。我有一个输入字段,其中name =“file”与此表单一起发送。

请帮助我诊断我的代码,以便我可以理解为什么会发生这种情况。谢谢。

<div class="container">
<div class="row section-register">
    <div class="col-md-8 col-md-offset-2">
        <div class="panel panel-default">
            <div class="panel-heading">New Auction</div>
            <div class="panel-body">
                <form class="form-horizontal" role="form" method="POST" action="{{ url('/addproduct') }}">
                    {{ csrf_field() }}
                    <div class="form-group">
                        <label for="itemname" class="col-md-2 control-label">Item name</label>

                        <div class="col-md-10">
                            <input id="itemname" type="text" class="form-control" name="itemname" value="" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="itemdesc" class="col-md-2 control-label">Decsription</label>

                        <div class="col-md-10">
                            <textarea id="itemdesc" class="form-control" name="itemdesc" value="" required></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="category" class="col-md-2 control-label">Category</label>

                        <div class="col-md-10">
                            <select id="category" class="form-control" name="category" required>
                                <option value="Games">Games</option>
                                <option value="Gadgets">Gadgets</option>
                                <option value="Books">Books</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="bid" class="col-md-2 control-label">Starting bid</label>

                        <div class="col-md-10">
                            <input id="bid" type="number" class="form-control" name="bid" value="" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="increment" class="col-md-2 control-label">Incremental value</label>

                        <div class="col-md-10">
                            <select id="increment" class="form-control" name="increment" required>
                                <option value="10">10</option>
                                <option value="20">20</option>
                                <option value="50">50</option>
                                <option value="100">100</option>
                                <option value="200">200</option>
                                <option value="300">300</option>
                                <option value="500">500</option>
                                <option value="1000">1000</option>
                            </select>
                        </div>
                    </div>
                    <br>
                    <p class="text-center warning-img">Images uploaded to SpinkyDink are resized. For the best quality images on your auction, please use land-scape images instead of portrait images.
                    <br>(Turn your phone sideways and snap a pic)</p>
                    <br>
                    <div class="form-group">
                        <label for="file" class="col-md-2 control-label">Main Image</label>

                        <div class="col-md-10 text-center">
                            <input id="file" type="file" class="form-control" name="file" required>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-2">
                            <input id="file2" type="file" class="form-control" name="file2">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-2">
                            <input id="file3" type="file" class="form-control" name="file3">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-2">
                            <input id="file4" type="file" class="form-control" name="file4">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-12 text-center">
                            <button type="submit" class="btn btn-success">
                                Submit Auction
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

途径:

Route::post('/addproduct', 'NewProductController@addproduct');

控制器

public function addproduct(Request $request)
{
    if ($request->hasFile('file')) {
        echo "file found";
    }else{
        echo "no file";
    }
}

当我点击此控制器时,每次都会返回行回显“无文件”。

1 个答案:

答案 0 :(得分:2)

将enctype属性添加到表单:

<form class="form-horizontal" role="form" method="POST" action="{{ url('/addproduct') }}" enctype="multipart/form-data">

默认情况下,数据会被编码,但如果您想使用表单上传文件,则必须禁用编码,这就是multipart / form-data的作用。