【问题标题】:Dropzone.JS in form with Laravel 5.2 not saving file带有 Laravel 5.2 的 Dropzone.JS 不保存文件
【发布时间】:2017-08-27 04:57:35
【问题描述】:

enctype="multipart/form-data" 在使用 DropzoneJS 将上传的文件保存到数据库时遇到了这个问题。

这种情况下的错误是它发布了所有其他表单数据,但上传的文件返回空响应,尽管我不确定我在这里缺少什么。

如果有人能设法发现错误,将不胜感激!

查看

<form action="{{url('/save_information')}}" method="POST" enctype="multipart/form-data" class="form-horizontal" role="form">
<meta name="csrf-token" content="{{csrf_token()}}" />
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
    <legend></legend>
</div>

<label for="title">Title</label>
<input type="text" name="title" id="input-title" class="form-control">

<label for="description">Description</label>
<input type="text" name="description" id="input-description" class="form-control">

<div class="dropzone dropzone-previews" id="my-awesome-dropzone">
    <input name="_token" type="hidden" value="{{ csrf_token() }}">
</div>

jQuery

<script type="text/javascript">
        Dropzone.autoDiscover = false;
        var myDropzone = new Dropzone("#my-awesome-dropzone",{
            url: "/save_information",
            uploadMultiple: true,
            autoProcessQueue: false,
            addRemoveLinks: true,
            paramName: "file", // The name that will be used to transfer the file
            maxFilesize: 10, // MB
            sending: function(file, xhr, formData) {
                formData.append("_token", "{{ csrf_token() }}");
            }
        });


        $('#submit-all').click(function(){
            e.preventDefault();
            myDropzone.processQueue();
        });

控制器功能

 if (Request::hasFile('filename')) {
        $image = Request::file('filename');
        $location = public_path('images/');
        $filename = time() . '.' . $image->getClientOriginalExtension();
            $image->move($location, $filename);

            $trustDocument->image = $filename;

        } else {
        dd('no image found');
    }

$trustDocument->save();

【问题讨论】:

  • enctype="multipart/form-data"

标签: javascript php jquery laravel dropzone.js


【解决方案1】:

您设置的 paramName 是“file”,但在您的控制器中您引用的是“filename”。

将控制器中的“文件名”替换为“文件”。

更详细的 laravel dropzone 集成可以参考这篇教程Integrating Dropzone.js in Laravel 5 applications

【讨论】:

    猜你喜欢
    • 2016-08-23
    • 2014-10-26
    • 2016-05-23
    • 1970-01-01
    • 2017-01-20
    • 2018-01-01
    • 1970-01-01
    • 2019-03-31
    • 2016-11-25
    相关资源
    最近更新 更多