Search code examples
laravellaravel-8dropzone.jsdropzone

Dropzonejs object object error. How can I fix it?


Files can't be upload to directory and give back error

js and css from dropzone is included in head and footer properly

Display on blade

Network

Dropzone js code

enter image description here

enter image description here

Balde code::

@include('Templates.head')
<body style="background: #283747;">
    @include('Menu.menu')
    @if (Session::has('user') && Session::has('privlages') && Session::get('privlages') >= '1')
    <div class="container">
        <div class="row">
            <form action="/gallery_add_image" class="dropzone" style="margin-left: 10%; margin-right: 10%;">
                @csrf
                <div class="fallback">
                  <input name="file" type="file" multiple />
                </div>
            </form>
        </div>
    </div>
    @else
    <script>window.location = "/";</script>
    @endif
@include('Templates.footer')

Controller code::

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class GalerryController extends Controller
{
    public function add_image(Request $add_image){
        $image = $add_image->file('file');
        $imageName = time(). '.' . $add_image->extension();
        $image->move(public_path('gallery'), $imageName);
        return response()->json(['success' => $imageName]);
    }
}

web(routing) code::

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\GalerryController;

Route::post('/gallery_add_image', [GalerryController::class, 'add_image']);

Server response code::

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

How can I fix this problem?


Solution

  • In your controller code $add_image is a request object, which does not have an extension method. To get the uploaded file extension you need to use:

    class GalerryController extends Controller
    {
        public function add_image(Request $add_image) {
            $add_image->validate([ 'file' => 'required|image' ]); // Good idea to validate
            $image = $add_image->file('file');
            $imageName = time(). '.' . $image->getClientOriginalExtension();
            $image->move(public_path('gallery'), $imageName);
            return response()->json(['success' => $imageName]);
        }
    }