- 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
- /
- .NET
- /
- Image Server Delete
.NET 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('/FroalaApi/DeleteImage', {
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 image path available in the HttpContext global variable.
The FroalaEditor.Image.Delete() method from the .NET SDK expects
the path of the image to remove from disk.
Note: The path is relative to the project root.
FroalaEditor.Image.Delete("/Public/" + HttpContext.Request.Form["src"]);
Complete Example
<script>
// Ask the server to delete an image file.
function deleteImage (src) {
fetch('/FroalaApi/DeleteImage', {
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>
using System;
using System.Web.Mvc;
namespace demo.Controllers
{
public class FroalaApiController : Controller
{
public ActionResult DeleteImage()
{
try
{
FroalaEditor.Image.Delete("/Public/" + HttpContext.Request.Form["src"]);
return Json(true);
}
catch (Exception e)
{
return Json(e);
}
}
}
}
Do you think we can improve this article? Let us know.