- Installation Guides
- Browser Support
- Languages Support
- Shortcuts
- Activation
- Examples
- Customize the Editor
- Use-cases
- Plugins
- APIs
- Development Frameworks
- Server Integrations
- Server SDKs
- Migration Guides
- Changelog
- Tutorials
- Froala Docs
- /
- SDKs
- /
- Java
- /
- Image Server Delete
Java Image Server Delete
How it works
- Code intercepts the image removed or replaced in the editor.
- Removing an image sends to the server a request to delete the image from disk.
- Once the request reaches the server, it deletes the image.
Intercept image being removed
Removing an image from the editor triggers two events: image.beforeRemove and image.removed. You can use either of them to delete the image from the server, however it is recommended to use the second one, since at that point you make sure that the image was removed from the editable area.
Replacing an image triggers image.replaced. Starting with v5.5, it passes the previous image's src as a third argument, old_src, so you can delete the replaced file the same way.
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>
// Ask the server to delete an image file.
function deleteImage (src) {
fetch('/delete_image', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({ src: src })
})
.then(function (response) {
console.log(response.ok ? 'Image was deleted' : 'Image delete problem');
})
.catch(function (err) {
console.log('Image delete problem: ' + err);
});
}
new FroalaEditor('.selector', {
events: {
// An image was removed from the editor.
'image.removed': function ($img) {
deleteImage($img.attr('src'));
},
// An image was replaced. old_src is the image that was replaced (v5.5+).
'image.replaced': function ($img, response, old_src) {
deleteImage(old_src);
}
}
});
</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 thejavax.servlet.http.HttpServletRequest parameter. The
com.froala.editor.Image.delete method from the Java SDK expects the path of the image to remove from
disk.
Note: The path is relative to the project root.
String src = request.getParameter("src");
Image.Delete(request, src);
Complete Example
<script>
// Ask the server to delete an image file.
function deleteImage (src) {
fetch('/delete_image', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({ src: src })
})
.then(function (response) {
console.log(response.ok ? 'Image was deleted' : 'Image delete problem');
})
.catch(function (err) {
console.log('Image delete problem: ' + err);
});
}
new FroalaEditor('.selector', {
events: {
// An image was removed from the editor.
'image.removed': function ($img) {
deleteImage($img.attr('src'));
},
// An image was replaced. old_src is the image that was replaced (v5.5+).
'image.replaced': function ($img, response, old_src) {
deleteImage(old_src);
}
}
});
</script>
package com.froala.examples.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.froala.editor.Image;
import com.google.gson.Gson;
/**
* Servlet implementation class DeleteImage
*/
@WebServlet("/delete_image")
public class DeleteImage extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DeleteImage() {
super();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String src = request.getParameter("src");
try {
Image.delete(request, src);
} catch (Exception e) {
e.printStackTrace();
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
String jsonResponseData = new Gson().toJson("Success");
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(jsonResponseData);
}
}
Do you think we can improve this article? Let us know.