Use async function in preUpload

Use async function in preUpload

geonovationgeonovation Posts: 5Questions: 3Answers: 0

Hello,

I want to change the upload file with an async function. I tried this code below but this isn't working. Is there a solution for this problem that I have?

datatableEditor.on( 'preUpload', function ( e, fieldName, file, ajaxData ) {

    loadImage(file, { maxWidth: 300 }).then(function (image) {
        ajaxData.set('upload', image);
    })
});

datatableEditor.on( 'preUpload', async function ( e, fieldName, file, ajaxData ) {

    // check image and resize image
    const image = await loadImage(file, {maxWidth: 300});

    ajaxData.set('upload', image);
});

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 is no provision of an async function in the preUpload event. That said, preUpload is only intended for use to return a boolean value to say the file should be uploaded to the server or not.

    It sounds like in this case you could do:

    datatableEditor.on( 'preUpload', function ( e, fieldName, file, ajaxData ) {
        loadImage(file, { maxWidth: 300 }).then(function (image) {
            ajaxData.set('upload', image);
        });
    
        return false;
    });
    

    I don't see any reason why that won't work (assuming loadImage gives the promise a string?).

    Allan

This discussion has been closed.