首页 > 解决方案 > 无法让 Dropzone 与 Laravel 一起工作

问题描述

使用 Laravel 框架 6.18.0

我确定这很简单,但我似乎无法弄清楚。我没有看到控制器中的函数被调用来实际完成将文件移动到服务器的工作。我已经附上了下图的链接,因为 dropzone 似乎正在完成它的工作,但路由/控制器端似乎失败了。

我的路线:

    Route::get('/uploadpics', 'UploadPicController@index');
    Route::post('/upload','UploadPicController@uploadFiles');

上传图片.blade.php:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="{{asset('js/plugins/dropzone/dist/min/dropzone.min.css')}}">
    <script src="{{asset('js/plugins/dropzone/dropzone.min.js')}}" type="text/javascript"></script>
</head>
<body>

<div class="content">
    <form method="post" action="{{url('/upload')}}" enctype="multipart/form-data" class="dropzone" id="dropzone">
        @csrf
    </form>
</div>

<script>

    Dropzone.autoDiscover = false;
    var myDropzone = new Dropzone(".dropzone",{
        maxFilesize: 3,  // 3 mb
        acceptedFiles: ".jpeg,.jpg,.png",
    });

</script>
</body>
</html>

我的 UploadPicController.php

<?php

namespace App\Http\Controllers;

use App\Coin;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Carbon\Carbon;

class UploadPicController extends Controller
{

    public function index(){
        return view('pages.uploadpics');
    }

    public function uploadFiles(Request $request){
        dd("i'm in upload");                            //**I never get here...**
        if($request->hasFile('file')) {

            // Upload path
            $destinationPath = 'images/';

            // Create directory if not exists
            if (!file_exists($destinationPath)) {
                mkdir($destinationPath, 0755, true);
            }

            // Get file extension
            $extension = $request->file('file')->getClientOriginalExtension();

            // Valid extensions
            $validextensions = array("jpeg","jpg","png");

            // Check extension
            if(in_array(strtolower($extension), $validextensions)){

                // Rename file
                $fileName = str_slug(Carbon::now()->toDayDateTimeString()).rand(11111, 99999) .'.' . $extension;

                // Uploading file to given path
                $request->file('file')->move($destinationPath, $fileName);
            }
        }
    }
}

我可以在页面上使用 dropzone 而不会出现错误,但它不会将文件复制到服务器 - 这就像控制器中的函数永远不会被调用(参见 dd(); - 从不命中)。

在此处输入图像描述

标签: laraveldropzone.js

解决方案


推荐阅读