Days
Hours
Minutes
Seconds
x

Froala Editor v4.2.1 is HereLEARN MORE

Skip to content

Concepts

Image Upload

By default the Froala Rich Text Editor stores the images as a Blob in the browser memory. We recommend that you upload them to your own server. In this case you have to handle the image upload and storage using a server-side language yourself. This tutorial is meant to help you understand how the editor's image upload works.

Upload Flow

  1. Set the editor's image upload options.

    When uploading images, you can customize up to 4 parameters: imageUploadParamimageUploadURLimageUploadParams and editable.imageError.
    imageUploadParam is the name of the parameter that contains the image file information in the upload request. The default value is "file" but you can change it to whatever name you want.
    imageUploadURL is the URL where the upload request is being made.
    imageUploadParams are additional params that are passed in the upload request to the server.
    editable.imageError event is triggered if any errors occur during the upload process.


  2. AJAX request from editor to the imageUploadURL link.

    When an image is inserted into the WYSIWYG HTML editor, a HTTP request is automatically done to the server.


  3. The server processes the HTTP request.

    The server has to process the upload request, save the image and return a JSON containing a link to the uploaded image. The returned JSON needs to look like: { link: 'path/to/image.jpg' }

    If you need help with the server side image upload process please read the Image Upload with PHP server integration example.


  4. The editor loads the image in browser.

    When the request finishes, the editor will load the image. If the returned hashmap does not follow the right structure or the image cannot be loaded, editable.imageError event is triggered.

Note: Returning a JSON with an error field will throw an error with the custom message contained. E.g. { error: 'My custom error message.' }


The following snippet highlights how to initialize the Froala WYSIWYG HTML editor in order to upload images.

<script>
  $(function() {
    $('.selector')
      .editable({
        // Set the image upload parameter.
        imageUploadParam: 'image_param',

        // Set the image upload URL.
        imageUploadURL: '/upload_image',

        // Additional upload params.
        imageUploadParams: {id: 'my_editor'}
      })
      .on('editable.imageError', function (e, editor, error) {
        // Custom error message returned from the server.
        if (error.code == 0) { ... }

        // Bad link.
        else if (error.code == 1) { ... }

        // No link in upload response.
        else if (error.code == 2) { ... }

        // Error during image upload.
        else if (error.code == 3) { ... }

        // Parsing response failed.
        else if (error.code == 4) { ... }

        // Image too text-large.
        else if (error.code == 5) { ... }

        // Invalid image type.
        else if (error.code == 6) { ... }

        // Image can be uploaded only to same domain in IE 8 and IE 9.
        else if (error.code == 7) { ... }
      });
  });
</script>