Use async function in preUpload

Use async function in preUpload

geonovationgeonovation Posts: 3Questions: 2Answers: 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);
});

Answers

  • allanallan Posts: 54,186Questions: 1Answers: 8,456 Site admin

    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

Sign In or Register to comment.