0

I am trying to delete image from folder using ajax and in route using delete method .In controller trying to delete image using image name in laravel.

Route:

Route::delete('remove-social/{filename}', 'Webadmin\Socials@removesocial');

Controller:

 public function removesocial($filename){
          File::delete('public/assets/uploads/Social/' . $filename);
   }

View :

 <a href="javascript:removesocialimage()" style="color: white;text-decoration: none;" class="btn btn-red">
                                <i class="glyphicon glyphicon-trash "></i> Remove</a>   </label>
    <script>
         function removesocialimage() {
                if (j('#file_name').val() != '')
                    if (confirm('Are you sure want to remove social icon?')) {
                        j('#loading').css('display', 'block');
                        var form_data = new FormData();
                        form_data.append('_method', 'DELETE');
                        form_data.append('_token', '{{csrf_token()}}');
                        j.ajax({
                            url: "remove-social/" + j('#file_name').val(),
                            data: form_data,
                            type: 'POST',
                            contentType: false,
                            processData: false,
                            success: function (data) {
                              j('#preview_image').attr('src', '{{URL::to('/public/assets/Webadmin/images/attach-1.png')}}');
                                j('#file_name').val('');
                                j('#loading').css('display', 'none');
                            },
                            error: function (xhr, status, error) {
                                alert(error);
                                alert(xhr.responseText);
                            }
                        });
                    }
            }
        </script>    
3
  • can u get any error ? Commented Nov 21, 2018 at 8:34
  • Please provide more context to your question, what happens now? Do you get any error? Commented Nov 21, 2018 at 8:34
  • it's not deleting image from folder as iam passing path for image to delete with in controller Commented Nov 21, 2018 at 10:02

2 Answers 2

1

Include this in your views head:

<meta name="csrf-token" content="{{ csrf_token() }}">

And do this ajax setup before making network calls:

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

i have added it in header layout
0

try to change route delete to post method

Route::post('remove-social/{filename}', 'Webadmin\Socials@removesocial');

if you want to used delete method then your ajax look like

add in head html tag

<meta name="csrf-token" content="{{ csrf_token() }}">

js code

 j.ajax({
       url: "remove-social/" + j('#file_name').val(),
       data: form_data,
       headers: {
            X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
       },
       type: 'DELETE',   //POST TO DELETE
       contentType: false,
       processData: false,
       success: function (data) {
       j('#preview_image').attr('src', '{{URL::to('/public/assets/Webadmin/images/attach-1.png')}}');
       j('#file_name').val('');
       j('#loading').css('display', 'none');
      },
      error: function (xhr, status, error) {
         alert(error);
         alert(xhr.responseText);
       }

});

1 Comment

i have implemented this but still not deleting image from folder.I am trying to delete image using image name to delete it from folder

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.