Days
Hours
Minutes
Seconds
x

New Froala Editor v4.3.1 is here – Learn More

Skip to content

Node.JS Image Server Delete

How it works

  1. Code intercepts the image removed from the editor.
  2. Removing an image sends to the server a request to delete the image from disk.
  3. Once the request reaches the server, it deletes the image.

Jump to Complete Example

Intercept image being removed

Removing an image from the editor triggers two events: froalaEditor.image.beforeRemove and froalaEditor.image.removed. You can use either of them to delete the image from the server, however it is recomended to use the second one, since at that point you make sure that the image was removed from the editable area.

Send request to the server

There is no built-in feature that makes the request to the server, however you can use an AJAX request.

<script>
  // Catch the image being removed.
  var editor = new FroalaEditor('.selector');

  editor.opts.events['image.removed'] = function (e, editor, $img) {
    $.ajax({
      // Request method.
      method: 'POST',

      // Request URL.
      url: '/delete_image',

      // Request params.
      data: {
        src: $img.attr('src')
      }
    })
    .done (function (data) {
      console.log ('Image was deleted');
    })
    .fail (function (err) {
      console.log ('Image delete problem: ' + JSON.stringify(err));
    })
  }
</script>

Delete the image

The server implementation is responsible for receiving the request and handling it appropriately. The code on the previous step makes the file path available in the body of the request: request.body.src. The FroalaEditor.Image.delete() method from the Node.JS SDK expects the path of the image to remove from disk.

Note: The path of the image is relative to the root of the application.

FroalaEditor.Image.delete(req.body.src, function(err) { ... });

Complete Example

<script>
  // Catch the image being removed.
  var editor = new FroalaEditor('.selector');

  editor.opts.events['image.removed'] = function (e, editor, $img) {
    $.ajax({
      // Request method.
      method: 'POST',

      // Request URL.
      url: '/delete_image',

      // Request params.
      data: {
        src: $img.attr('src')
      }
    })
    .done (function (data) {
      console.log ('Image was deleted');
    })
    .fail (function (err) {
      console.log ('Image delete problem: ' + JSON.stringify(err));
    })
  }
</script>
var express = require('express');
var app = express();
var bodyParser = require('body-parser')
var path = require('path');
var fs = require('fs');
var FroalaEditor = require('PATH_TO_FROALA_SDK/lib/froalaEditor.js');

app.use(express.static(__dirname + '/'));
app.use('/bower_components',  express.static(path.join(__dirname, '../bower_components')));
app.use(bodyParser.urlencoded({ extended: false }));

// Listen to the delete request.
app.post('/delete_image', function (req, res) {

  // Do delete.
  FroalaEditor.Image.delete(req.body.src, function(err) {

    if (err) {
      return res.status(404).end(JSON.stringify(err));
    }

    return res.end();
  });
});

Do you think we can improve this article? Let us know.