Skip to main content

Command Palette

Search for a command to run...

Use DropzoneJS with Laravel8

Updated
1 min read
Use DropzoneJS with Laravel8

1. Download and install

<link href="/css/dropzone.min.css" rel="stylesheet">
<script src="/js/dropzone.min.js"></script>

2. Add class to a div

<input type="hidden" name="photo" id="photo">
<div id="upload" class="dropzone"></div>

image.png

3. Create options for DropzoneJS

Dropzone.options.upload = {
        url: "/upload",
        paramName: "file",
        maxFilesize: 2,
        maxFiles: 1,
        acceptedFiles: ".jpeg,.jpg,.png,.gif",
        sending: function(file, xhr, formData) {
            formData.append("_token", "{{ csrf_token() }}");
        },
        success: function(file, response) {
            document.getElementById('photo').value = response.data.url;
        }

    };

4. Save the file on the backend

public function upload(Request $request)
    {
        $file = $request->file('file');
        $fileName = time() . '.' . $file->extension();
        $file->move(public_path('images'), $fileName);
        return response()->json(['message' => 'success', 'data' => ['url' => $fileName]]);
    }
\n2. Add class to a div\n\n
\n\n3. Create options f...","author":{"@type":"Person","name":"Eric Li","url":"https://hashnode.com/@mendaxia","image":"https://cdn.hashnode.com/res/hashnode/image/upload/v1617859039627/3Paz3EmCP.jpeg"},"publisher":{"@type":"Organization","name":"Up in the Air","url":"https://blog.ericli.ca","logo":"https://cdn.hashnode.com/res/hashnode/image/upload/v1616998212183/iDAeqYfBv.png"},"datePublished":"2021-04-08T05:16:47.560Z","dateModified":"2021-04-08T05:28:58.204Z","image":{"@type":"ImageObject","url":"https://cdn.hashnode.com/res/hashnode/image/upload/v1617859726742/pw7_AiJML.png"},"keywords":"Laravel"}