vandan's avatar
Level 13

how to image upload using dropzone ?

i dont know how to use js and upload image using dropzone

here is my code

<form action="{{route('sign.store')}}" method="POST" enctype="multipart/form-data" >
{{csrf_field()}}
<div class="col-lg-7 col-md-9 col-sm-12">
        <div class="dropzone dropzone-default dz-clickable" id="kt_dropzone_1">
                <div class="dropzone-msg dz-message needsclick">
                    <h3 class="dropzone-msg-title">Drop files here or click to upload.</h3>
        </div>
             </div>
         </div>
<input type="text" class="form-control" placeholder="Enter zodiac name">
<input type="submit">
</form>

here is my js file

var demo1 = function () {
        $('#kt_dropzone_1').dropzone({
                url: "https://keenthemes.com/scripts/void.php",
                paramName: "file",
            maxFiles: 1,
             maxFilesize: 5,
                addRemoveLinks: true,
                accept: function(file, done) {
                    if (file.name == "justinbieber.jpg") {
                            done("Naha, you don't.");
                    } else {
                            done();
                    }
                }
        });
0 likes
7 replies
vandan's avatar
Level 13

@bobbybouwmann sir i try this tutorial but when i try to submit using form in my html page design was change and try to return image null

vandan's avatar
Level 13

@jeffreyvanrossum but in my case i done already but when i check in controller $request->file('file'); then null no image fetch

bobbybouwmann's avatar

Your form looks correct, but the URL in dropzone itself is incorrect. Can you post what you have now?

vandan's avatar
Level 13

@bobbybouwmann sir

here is my blade file code

<form action="{{url('dropzone/store')}}" method="POST" enctype="multipart/form-data">
{{csrf_field()}}
    <div class="dropzone dropzone-default dz-clickable" id="kt_dropzone_1">
                <div class="dropzone-msg dz-message needsclick">
                        <h3 class="dropzone-msg-title">Drop files here or click to upload.</h3>
                </div>
        </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

<script type="text/javascript">
        Dropzone.options.dropzone =
        {
            maxFilesize: 10,
            renameFile: function (file) {
                    var dt = new Date();
                    var time = dt.getTime();
                return time + file.name;
            },
            acceptedFiles: ".jpeg,.jpg,.png,.gif",
            addRemoveLinks: true,
            timeout: 60000,
            success: function (file, response) {
                console.log(response);
            },
            error: function (file, response) {
                    return false;
            }
        };
</script>

here is my controller

public function store(Request $request)
    {
        $image = $request->file('file');
        $avatarName = $image->getClientOriginalName();
        $image->move(public_path('images'),$avatarName);
     
        $imageUpload = new Image();
        $imageUpload->filename = $avatarName;
        $imageUpload->save();
        return response()->json(['success'=>$avatarName]);
    }

this is my js code

 $('#kt_dropzone_1').dropzone({
        url: "/store", // Set the url for your upload script location
        paramName: "file", // The name that will be used to transfer the file
        maxFiles: 1,
        maxFilesize: 5, // MB
        addRemoveLinks: true,
        accept: function(file, done) {
            if (file.name == "sign.jpg") {
                done("Naha, you don't.");
            } else {
                done();
            }
        }
    });

Please or to participate in this conversation.