- Back to Docs
- Install and Configure
- Image
- Server Upload
- Server Delete
- S3 Upload
- Resize
- Validation
- Image Manager
- Simple Server
- File
- Server Upload
- Server Delete
- S3 Upload
- Validation
- References
- Image
- File
- S3
Node.JS Image Server Upload
How it works
- You specify the upload options when the rich text editor is initialized on client side.
- When an image is inserted, the javascript editor automatically makes an AJAX request to the server.
- Once the request hits the server, it stores the image and sends back to the client the link to the uploaded image.
Initialize the javascript editor
To get started, the rich text editor needs to have the imageUploadURL
option set as the URL where you want to have the images uploaded. Additionally, you can set other options related to the way the images are being uploaded and what data your server receives: imageUploadParam
, imageUploadParams
, imageUploadMethod
, imageMaxSize
, imageAllowedTypes
.
<script> $(function() { $('.selector').froalaEditor({ // Set the image upload URL. imageUploadURL: '/upload_image' }) }); </script>
Receive the uploaded image and store it
The server implementation is responsible for receiving the request and handling it appropriately. In Node.JS, the uploaded image is available in the request
received by your application. The Node.JS editor SDK is designed to automatically detect the uploaded image, so you just have to specify the path where to store it.
Note: The path of the image is relative to the root of your application.
FroalaEditor.Image.upload(req, '/uploads/', function (err, data) { ... });
For the uploaded image to be stored correctly, the server should have the rights to write in the uploads folder. Additionally, you should check that the uploaded image is public accessible in browser so it can be displayed to your users.
Return the path to the uploaded image
If the save action is completed successfully, the SDK is generating an ImageResponse
object with the absolute path to the uploaded image, so your server just have to return it back to the client side.
if (err) { return res.send(JSON.stringify(err)); } res.send(data);
Complete Example
<script> $(function() { $('.selector').froalaEditor({ // Set the image upload URL. imageUploadURL: '/upload_image' }) }); </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 })); // Path to upload image. app.post('/upload_image', function (req, res) { // Store image. FroalaEditor.Image.upload(req, '/uploads/', function(err, data) { // Return data. if (err) { return res.send(JSON.stringify(err)); } res.send(data); }); });