Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show success/error message on dropzone file upload

I am using dropzone file upload, when I return success / error from controller, how do I show it on my view file..

View file ,

 <div class="box-body">
    @if ( $errors->count() > 0 )
    <div class="alert alert-danger alert-dismissible">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        {{ $message = 'Please select csv file only.' }}
    </div>
    @endif

{!! Form::open([ 'url' => 'admin/reports','class' => 'dropzone', 'id' => 'reportfile' ]) !!}
    {!! csrf_field() !!}
    <div class="col-md-12">
        <h3 style="text-align : center">Select File</h3>
    </div>
    <button type="submit" class="btn btn-primary">Upload Report</button>
    {!! Form::close() !!}

</div>

Controller Code

if ($request->hasFile('file')) {
        $file = Input::file('file');
        $validator = Validator::make(
                        [
                    'file' => $file,
                    'extension' => strtolower($file->getClientOriginalExtension()),
                        ], [
                    'file' => 'required',
                    'extension' => 'required|in:csv',
                        ]
        );
        if ($validator->fails()) {

            return Response::json('error', 400);

        }

JS Code

<script>
window.onload = function () {

    Dropzone.options.reportfile = {
        paramName: "file", 
        maxFilesize: 2,
        error: function (file, response) {
            alert('hiii')
        },
        init: function () {
            this.on("addedfile", function (file) {
                alert("Added file.");
            });
        },
        accept: function (file, done) {
            alert('hi')
            if (file.name == "justinbieber.jpg") {
                done("Naha, you don't.");
            }

        }

    };
};
</script>

Not even alert is working...any help is much appreciated..Thanks

like image 687
Aamir Avatar asked Oct 15 '25 16:10

Aamir


1 Answers

You should listen the events, and trigger whether it is success or not

Dropzone.options.uploadWidget = {
  init: function() {
    this.on('success', function( file, resp ){
       alert("Success");
    });
  },
  ...
};

Note : You can have the other breakpoints as suggested here

like image 173
Sulthan Allaudeen Avatar answered Oct 18 '25 05:10

Sulthan Allaudeen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!