I am currently working on a project which need an upload file input which I implemented based on some example I saw on this site and it works fine.
function send(file){
var fd = new FormData();
fd.append(file.name, file);
$.ajax({
url: 'upload',
type: 'POST',
enctype: "multipart/form-data",
xhr: function xhr() {
myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
myXhr.upload.addEventListener('progress', function(evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
percentComplete = parseInt(percentComplete * 100, 10);
progress.setValue(percentComplete);
}
}, false);
}
return myXhr;
},
beforeSend: beforeSend,
success: onComplete,
error: onError,
// Form data
data: fd,
//Options to tell jQuery not to process data or worry about content-type.
cache: false,
contentType: false,
processData: false
});
}
However One thing is not working well and I am curious to know if someone has a great explanation for that. On the java server side my servlet check if the user is allowed to upload files and if not send back a success=false or what ever. The curious thing is that the progress bar is still progressing like if the server will upload it and I got only the error when the progress is finish. That is really strange my expectation was that the progress will not start or should be canceled on error returned. For sure I could check the user right before starting sending but I did a second an put a wrong URL in the ajax for which there is no end point and it still showing the progression until the end which if it is a big file take a while and sometimes freeze the browser. Is there a way to stop the progression earlier as the and of the progress on server error. Would be great to find a solution after a couple of hours googling not founding someone having the same issue Regards JP