使用Laravel 5添加DropzoneJS以进行文件上载

时间:2015-06-12 15:08:36

标签: php laravel laravel-5 dropzone.js

我有一个文件上传输入的表单运行良好,但我想用DropzoneJS替换它以添加拖放功能,但任何文件都使用DropzoneJS上传。

这就是我的看法:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Testing DropzoneJS with Laravel 5</title>


<script src="{{ asset('js/dropzone.js') }}"></script>
<link rel="stylesheet" href="{{ asset('css/dropzone.min.css') }}">

</head>
<body>

<div class="container">
            <form class="form-horizontal dropzone dz-clickable" action="{{url('/upload)}}" method="post"  enctype="multipart/form-data">
            <!-- Name input-->

            <input id="name" name="name" type="text" placeholder="Your Name" class="form-control">

            <div class="dz-message" id="my-dropzone">
            <h4>Drag Photos to Upload</h4>
            <span>Or click to browse</span>
            </div>

            <!-- Token -->
            <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
            <!-- Form actions -->

            <button type="submit" class="btn btn-primary btn-lg">Add</button>
          </form>
</div>

</body>
</html>

我的路线档案:

Route::post('/upload', function(){
    $the_new_product->name = Input::get('name');
    $the_new_product_picture = Input::file('file');

    return dd($the_new_product_picture); // I get Null as respons 
});

我得到Null作为响应,所以我猜文件没有上传。 我错过了什么吗?

1 个答案:

答案 0 :(得分:2)

好吧,我尝试了dropzone.js并且速度非常快,一切正常。

在查看您的代码之后,我想我已经找到了您的问题:

route.php 上,你有这个:

Route::post('/upload', function(){
    $the_new_product->name = Input::get('name');
    $the_new_product_picture = Input::file('file');

    return dd($the_new_product_picture); // I get Null as respons 
});

这是错误的,您看到要获取文件时必须使用请求而非输入,Laravel 4.2上使用了输入。

只需复制并粘贴此内容,然后在 routes.php

上覆盖上传路线
Route::post('/upload', function () {
    //check if file was uploaded
    if (Request::hasFile('file'))
    {
        //houston we have a file!
        $file = Request::file('file');

        //move it to our public folder and rename it to $name
        Request::file('file')->move('images', 'insert_file_name.'.$file->getClientOriginalExtension());
        echo 'file uploaded!';
        var_dump($file);
    }else{
        echo 'no file, no bueno';
    }
});

就是这样!如果您想了解有关获取表单输入和文件的更多信息,请阅读Request documentation,因为您通常要验证该输入,请查看Validation documentation

修改 这是我的观点:

<!DOCTYPE html>
<html>
    <head>
        <title>Laravel</title>

        <link href='//fonts.googleapis.com/css?family=Lato:100' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" type="text/css" href="{{ asset('css/dropzone.css') }}">
        <script type="text/javascript" src="{{ asset('js/dropzone.js') }}"></script>

        <style>
            html, body {
                height: 100%;
            }

            body {
                margin: 0;
                padding: 0;
                width: 100%;
                color: #B0BEC5;
                display: table;
                font-weight: 100;
                font-family: 'Lato';
            }

            .container {
                text-align: center;
                display: table-cell;
                vertical-align: middle;
            }

            .content {
                text-align: center;
                display: inline-block;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="content">
                 <form class="form-horizontal dropzone dz-clickable" action="{{url('/upload')}}" method="post"  enctype="multipart/form-data">
                    <!-- Name input-->

                    <input id="name" name="name" type="text" placeholder="Your Name" class="form-control">

                    <div class="dz-message" id="my-dropzone">
                    <h4>Drag Photos to Upload</h4>
                    <span>Or click to browse</span>
                    </div>

                    <!-- Token -->
                    <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
                    <!-- Form actions -->

                    <button type="submit" class="btn btn-primary btn-lg">Add</button>
                  </form>
            </div>
        </div>
    </body>
</html>