0

Hi I have a form which is using dropzone.js as below,

<form method="post" action="{{url('/example/fileupload')}}" enctype="multipart/form-data" class="dropzone" id="dropzone">

        @csrf

        <input type="submit">

    </form>  

I am using below code to configure dropzone.js

<script type="text/javascript">
        Dropzone.options.dropzone =
         {
            maxFilesize: 12,
            acceptedFiles: ".jpeg,.jpg,.png,.gif",
            addRemoveLinks: true,
            timeout: 5000

};
</script>

Its working on front-end means it shows images uploading in just dropzone view. I want to ask how can I call my laravel controller function from here. That function has all the logic for renaming the image files and saving it in database. The function (named fileupload) should be called after I click the submit button. I have already created the routes as below,

//just output the view with dropzone area
    Route::get('/example','exampleController@show'); 

 //fileupload function contains all the logic of renaming and saving image iles
    Route::post('/example/fileupload','exampleController@fileupload');

1 Answer 1

0

Dropzone will automatically call your form action as soon as files are dropped at the dropzone. You do not need a submit button to submit the files to a server. You can verify this by opening Chrome browser DOM inspector and go to Network tab then drop files on the dropzone you will see some requests in the network tab. Your form may look like this.

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

You can rename files before sending to the server but it is not required.

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

On the server side you can store files like below.

use App\ImageUpload;

public function fileupload(Request $request)
    {
        $image = $request->file('file');
        $imageName = $image->getClientOriginalName();
        $image->move(public_path('images'),$imageName);

        $imageUpload = new ImageUpload();
        $imageUpload->filename = $imageName;
        $imageUpload->save();
        return response()->json(['success'=>$imageName]);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for replying irfan. So in javascript we don't need to call fileupload function?
No you can not call fileupload directly from Javascript but instead dropzone makes a post call for you to fileupload function. Javascript can make ajax calls to server side routes or urls and this is how server side code is called from client side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.