1

Trying to delete uploaded images via ajax post request on post edit form. But i am facing some issues, when i press on delete button - it successfully sends ajax post request with image path, then in my controller i am trying to delete that file, but it not deleting it.

Here's my js:

(function($) {
    $('.del-image').click(function() {
        var thisUrl = $('.col-md-10 img').attr('src');

        $.ajax({
            method: 'post',
            url: '/del-image',
            data: {imgUrl: thisUrl, id: pageId},
            error: function(e) {
                alert('Error' + e);
            }
        });
    });
})(jQuery);

Then in controller i am receiving ajax post request:

public function deleteImage() {
    if (Request::ajax()) {
        $path = Input::get('imgUrl');
        File::delete($path);
        return 'ajax request';
    }

    return App::abort(404);
}

And in routes.php i have:

Route::any('del-image', array('uses' => 'PageController@deleteImage'));

Any help would be appreciated!

18
  • Are you sure that AJAX request is being sent to your controller and what you get instead, any error messages ? Where your files are saved (in the public folder) ? Commented Jun 8, 2014 at 16:57
  • No error messages, in a console i see the message 'ajax request' when i click delete button. Commented Jun 8, 2014 at 16:59
  • Try try{ File::delete($path); return 'ajax request'; } catch(Exception $e) { dd($e); } Commented Jun 8, 2014 at 17:02
  • Also make sure that path is right and has write permission. Commented Jun 8, 2014 at 17:04
  • I didn't see success method in your AJAX call, how do you see 'ajax request' in the console. Commented Jun 8, 2014 at 17:05

1 Answer 1

1

Just try this (To get the file path for uploads/filename.ext):

var  = $('.col-md-10 img').attr('src');
var thisUrlArray = thisUrl.split('/');
thisUrl = 'uploads/' + thisUrlArray.pop();

Then use thisUrl as your imgUrl in data parameter of AJAX call.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, mate! I've used: var thisImg = thisUrl.replace('domain.com', '');

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.