Unable to override UPLOAD ajax
Unable to override UPLOAD ajax
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
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 theajax
object to after the defaults object, then yoursuccess
etc would take priority. It would still make an Ajax call though.Another option might be the
preUpload
event which is cancellable (returnfalse
from your event handler). It is passed in the file information, so that might be the best way to go in this case.Allan
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 withreturn false
. Then I edited the field manually.Here's the entire callback function, should anyone need it as an example:
Regards,
Rado.