添加 DropzoneJS 以使用 Laravel 5 上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30806424/
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 11:46:59  来源:igfitidea点击:

Add DropzoneJS for file upload with Laravel 5

phplaravellaravel-5dropzone.js

提问by Asme Just

I have a form with files uploading input that is working well but I want replace it with DropzoneJS to add drag and drop functionality but any files get uploaded with DropzoneJS.

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

This what my wiew looks like:

这是我的 wiew 的样子:

<!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>

my route file:

我的路线文件:

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 
});

I get Null as respons, so I guess the file is not uploaded. Am I missing something?

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

采纳答案by Fabio Antunes

Well I tried dropzone.js and it was was blazing fast and everything worked fine.

好吧,我尝试了 dropzone.js,它非常快,而且一切正常。

And after looking at your code, I think I figured out your problem:

看了你的代码后,我想我想出了你的问题:

On your route.phpyou have this:

在你的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 
});

This is wrong, you see when you want to fetch a file you must use Requestnot Input, Input was used on Laravel 4.2.

这是错误的,你看到当你想要获取一个文件时,你必须使用Request而不是Input,Laravel 4.2 上使用了 Input 。

Just copy and paste this and overwrite your upload route on your routes.php

只需复制并粘贴此内容并在您的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';
    }
});

And that's it! If you want to read more about getting form inputs and files you should read the Request documentationalso since you usually want to validate that input, take a look at Validation documentation

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

EDIT:Here's my View:

编辑:这是我的观点:

<!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>