Unable to override UPLOAD ajax

Unable to override UPLOAD ajax

Radoslav KonuchRadoslav Konuch Posts: 9Questions: 4Answers: 0
edited September 2018 in Editor

Hello, I'm struggling with overriding the upload ajax settings.

Use case: Bubble editor is used to save images to our DB. We don't actually upload images to our server, but instead we make a base64 hash out of them and send them in a plain POST request. So we dont need the real upload AJAX call that upload plugin provides.

Problem: I can't override the default ajax settings of the upload field. Currently when I select an image, upload request is automatically fired to the server, fails with 404 and bubble form is cleared.

What I would like to do, is to override the ajax settings, so I can call my own abstract function with a callback.

Field config:

type: 'upload',
display: ( data, type, row ) => {
    connsole.log()
    const decodedBase64 = window.atob(data);
    const decodedData = _.convertToType(decodedBase64, ColumnDataType.BINARY);
    const urlCreator = window.URL || (window as any).webkitURL;
    const imageUrl = urlCreator.createObjectURL(new Blob([decodedData], {type: 'image/png'}));
    return '<img class="ct-role-image" src="' + imageUrl + '"/>';
},
clearText: 'Clear',
noImageText: 'No image',
ajax: {
    success: function (json) {
        // fire my own callback from here and continue to display() method of the field
    },
    error: function (err) {
       // OR catch the error callback here and continue to display() method of the field
    },
}

How should I properly configure the ajax settings, so they dont get actualy overriden by the defaults?

Also, if there is a way to accomplish our use case using the available upload events, instead of modifing the ajax, we could go that way.

Thanks for you help in advance.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi,

    I'm afraid there isn't actually a way to override the default upload Ajax call at the moment. It always assumes that an Ajax call to the server will be made.

    You could modify the source a little - in Editor.upload you'll find $.ajax( $.extend( {}, ajax, { - if you move the ajax object to after the defaults object, then your success etc would take priority. It would still make an Ajax call though.

    Another option might be the preUpload event which is cancellable (return false from your event handler). It is passed in the file information, so that might be the best way to go in this case.

    Allan

  • Radoslav KonuchRadoslav Konuch Posts: 9Questions: 4Answers: 0

    Hi Alan,

    thanks for the hint. Went with the option #2. I kept the ajax property for the editor field as an empty json object, so editor does't complain about no ajax options.
    Then I've setup the preUpload callback to cancel the regular call with return false. Then I edited the field manually.
    Here's the entire callback function, should anyone need it as an example:

        public processPreUploadImage(file: File, fieldName: string, editor: any): any {
            const r = new FileReader();
            r.onload = () => {
                const binary = r.result;
                const fileBase64 = window.btoa(binary);
                // edit the field manually
                editor.field(fieldName).set(fileBase64);
            };
            r.readAsBinaryString(file);  
            // prevent regulat callback
            return false;
        }
    

    Regards,
    Rado.

This discussion has been closed.