mercredi 11 août 2021

upload csv with dropzone and laravel 5.6

I´m traying to upload csv file to import data with dropzone, with out form. I can arrive to my function controller, but i´m traying send file to this controller and always formData it´s empty... I don´t know that i´m doing wrong.

Maybe i´m not working correctly but i can show one example very symple for use dropzone.

This it´s my actual code in my blade:

html

<div id="dropzone">
  <div>Seleccionar fichero</div>
  <input type="file" accept=".csv" />
</div>

js

        $(function() {
  
            $('#dropzone').on('dragover', function() {
                $(this).addClass('hover');
            });
            
            $('#dropzone').on('dragleave', function() {
                $(this).removeClass('hover');
            });
            
            $('#dropzone input').on('change', function(e) {
                var file = this.files[0];
                var token = $("meta[name=csrf-token]").attr("content");

                $('#dropzone').removeClass('hover');

                let attached = new FormData();           
                attached.append("file", file);
                
                $.ajax({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    url: "",
                    type: "POST",
                    contentType: false,
                    processData: false,
                    enctype: 'multipart/form-data',   
                    data: { '_token': token, 'attached': attached },
                    success:function(response){
                        console.log(response);
                    },
                    error: function(xhr){
                        alert(xhr.responseText);
                    }
                })
            });
        });
    </script>

my content file it´s:

name: "GRANADA.csv", lastModified: 1556279733717, lastModifiedDate: Fri Apr 26 2019 13:55:33 GMT+0200 (hora de verano de Europa central), webkitRelativePath: "", size: 7059453, …}

and my formData it:

FormData {}
__proto__: FormData
append: ƒ append()
delete: ƒ delete()
entries: ƒ entries()
forEach: ƒ forEach()
get: ƒ ()
getAll: ƒ getAll()
has: ƒ has()
keys: ƒ keys()
set: ƒ ()
values: ƒ values()
constructor: ƒ FormData()
Symbol(Symbol.iterator): ƒ entries()
Symbol(Symbol.toStringTag): "FormData"
__proto__: Object

any idea that i´m doing bad? in my controller i have this:

$adjunto = $request->get('attached');
        $route = "";

        print_r($adjunto);
        exit();

this return empty result

thanks for help and read me



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire