0

I'm trying to add a common function for jquery blueimp file upload in a js file, so that I can use the same function for multiple file upload inputs. I'm calling the function as:

var fileoutput = attachfile(fileobject, ['xlsx','zip'],"tmp");

This will call the function :

function attachfile(fileobject, allowedfiletypesarray, destinationfolderpath) {
    var allowedFileTypes; 
    allowedFileTypes = allowedfiletypesarray.join('|');
    allowedFileTypes = "/\.("+allowedFileTypes+")$/i";

    $(fileobject).fileupload({
        add: function(e, data) {        
            if(data.originalFiles[0]['type'].length && !allowedFileTypes.test(data.originalFiles[0]['type'])) { 
                $('#fileerror').html('Not an accepted file type'); // show error message
                return false;
            } 
            data.submit();
        },
        url: "uploadcsv.php", 
        dataType: 'text',
        acceptFileTypes : allowedFileTypes,
        formData: {"upload_url":destinationfolderpath},
        done: function (e, data) {
            var fileuploadresult = data.result;
            fileuploadresult = JSON.parse(fileuploadresult); 
            console.log(fileuploadresult);
            return fileuploadresult;
        },
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');   
}

Now the issue is,

var fileoutput = attachfile(fileobject, ['xlsx','zip'],"tmp");
console.log(fileoutput);

This is returning undefined. And I'm not getting the return from attachfile(). console.log(fileuploadresult); inside attachfile() is printing the uploaded file details correctly.

Can anyone help me to solve this. Thanks in advance.

Jenz
  • 8,280
  • 7
  • 44
  • 77
  • Answered here. [Returning a response from an asynchronous call](https://stackoverflow.com/a/14220323/3585500) – ourmandave Dec 17 '18 at 13:08
  • If you've got babel and/or webpack going [here's a jquery example using async/await.](https://petetasker.com/using-async-await-jquerys-ajax/) – ourmandave Dec 17 '18 at 13:21
  • run a callback function that sets the value instead of return it from done: – john Smith Dec 17 '18 at 13:37

0 Answers0