Koha/koha-tmpl/intranet-tmpl/prog/js/file-upload.js
Jonathan Druart 6323e08ed7
Bug 36084: Fix file upload and www/batch.t
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2024-03-01 11:01:01 +01:00

33 lines
1.3 KiB
JavaScript

function AjaxUpload ( input, progressbar, xtra, callback ) {
// input and progressbar are jQuery objects
// callback is the callback function for completion
var formData= new FormData();
$.each( input.prop('files'), function( dx, file ) {
formData.append( "uploadfile", file );
});
formData.append( "csrf_token", $('meta[name="csrf-token"]').attr('content') );
var xhr= new XMLHttpRequest();
var url= '/cgi-bin/koha/tools/upload-file.pl?' + xtra;
progressbar.val( 0 );
progressbar.next('.fileuploadpercent').text( '0' );
xhr.open('POST', url, true);
xhr.upload.onprogress = function (e) {
var p = Math.round( (e.loaded/e.total) * 100 );
progressbar.val( p );
progressbar.next('.fileuploadpercent').text( p );
}
xhr.onload = function (e) {
var data = JSON.parse( xhr.responseText );
if( data.status == 'done' ) {
progressbar.val( 100 );
progressbar.next('.fileuploadpercent').text( '100' );
}
callback( data.status, data.fileid, data.errors );
}
xhr.onerror = function (e) {
// Probably only fires for network failure
alert('An error occurred while uploading.');
}
xhr.send( formData );
return xhr;
}