4

I am using jquery file upload and it works like a charm. But I got an error when I call send method without any files. I am using the send method which allows me to submit the files asynchronously. On the doc (ctrl-f send) it is said this method returns a jqXHR object that can be used later for callback like fail callback.

When I debug with the chrome debugger I can see that the callback fail is called but all the arguments (jqXHR, textStatus, errorThrown) are undefined. So I get this error in the console : undefined undefined undefined.

I've made this jsfiddle where you can see the bug, just click on the button send by fileupload send api and you'll see the error in the console (open it via F12 before).

Why the arguments are undefined? I think it's a bug...

Here is the javascript :

$(document).ready(function () {
    var filesList = []
    var elem = $("form")
    file_upload = elem.fileupload({
        autoUpload: false,
        fileInput: $("input:file"),
    }).on("fileuploadadd", function (e, data) {
        filesList.push(data.files[0])
    });

    $("button").click(function () {
        // fix
        if (filesList.length > 0) {
          file_upload.fileupload(
              'send', 
              {
                  files: filesList
              }
          )
          .fail(function (jqXHR, textStatus, errorThrown) {
              console.error(jqXHR, textStatus, errorThrown);
          })
        }
    })
});

EDIT : I think I need to check the number of files before trying to upload them. The error function is called but when I inspect the debugger I didn't see any requests to server. So I guess it's a bug, because the fail callback shoudn't be called.

1 Answer 1

1

I think the problem is that you call the method send even if your file list is empty.

You can't access jqXHR properties because none AJAX request is fired.

If you add a control based on the length of your list, your error callback will be called only when an error occurs with your AJAX request and so you will be able to retrieve the informations about the error.

Sign up to request clarification or add additional context in comments.

1 Comment

There is another workaround. I tested with {} instead of [], and I didn't get the error. So I can do a test like return (filesList.length > 0) ? filesList : {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.