spooler's avatar

Delete File From Dropzone

Hi, i want to delete file from dropzone and i have a problem.

i upload photos with following function:

    public function ImageSave(Request $r,$inputName) {
        if($r->hasFile($inputName))
        {
            $pic = $r->file($inputName);
            $name = $pic->getClientOriginalName();
            $filename = uniqid() . $name;
            $ext = $pic->move('uploads/products', $filename);
            $imageAddress = 'uploads/products/'.$filename;
            return $imageAddress;
        }
    }

when i upload new photo i added unique character before name of photo and i try to remove photo with following codes:

self.on("removedfile", function (file) {
                            $.ajax({
                                type: 'POST',
                                url: '/admin/productphoto/delete/',
                                data: {id: file.name, _token: $('input[name="_token"]').val()},
                                dataType: 'html',
                                success: function(data){
                                    var rep = JSON.parse(data);
                                    if(rep.code == 200)
                                    {
                                        photo_counter--;
                                        $("#photoCounter").text( "(" + photo_counter + ")");
                                        self._updateMaxFilesReachedClass();
                                    }

                                }
                            });

                        });

but when i passed file name for delete can not find photo because i added unique character before name

what should i do?

0 likes
8 replies
MaverickChan's avatar

no , your method type is wrong .

add ( _method : delete ) in your data.

and your url should add the file id at the end after /.

MaverickChan's avatar

follow tutorial below about photo upload and delete.

https://laracasts.com/series/build-project-flyer-with-me

when doing ajax , make sure the requst type is right . in your case , for example:

            $.ajax({
                                type: 'POST',
                                url: '/admin/productphoto/delete/',  // and i readlly think you should pass the file id.
                                data: {
                    id: file.name,
                     _token: $('input[name="_token"]').val()
                    _method : delete // add here
                    },

                                dataType: 'html',
                                success: function(data){
                                    var rep = JSON.parse(data);
                                    if(rep.code == 200)
                                    {
                                        photo_counter--;
                                        $("#photoCounter").text( "(" + photo_counter + ")");
                                        self._updateMaxFilesReachedClass();
                                    }

                                }
                            });


MaverickChan's avatar

when you save a file , it would be better saving the path to database.

when you want to delete them , just get the id , then you can get the full path of file .

use FILE to delete it.

actually , your post or delete request is for deleting the record in database.when it is done , you can add a line delete the file with full path.

spooler's avatar

@MaverickChan i saving the path to database but i don't know how to delete file

how to passed file id to controller?

MaverickChan's avatar

watch the video i gave you and you will find it very easy

Please or to participate in this conversation.