1 function AjaxUpload ( input, progressbar, xtra, callback ) {
2 // input and progressbar are jQuery objects
3 // callback is the callback function for completion
4 var formData= new FormData();
5 $.each( input.prop('files'), function( dx, file ) {
6 formData.append( "uploadfile", file );
8 var xhr= new XMLHttpRequest();
9 var url= '/cgi-bin/koha/tools/upload-file.pl?' + xtra;
11 progressbar.next('.fileuploadpercent').text( '0' );
12 xhr.open('POST', url, true);
13 xhr.upload.onprogress = function (e) {
14 var p = Math.round( (e.loaded/e.total) * 100 );
16 progressbar.next('.fileuploadpercent').text( p );
18 xhr.onload = function (e) {
19 var data = JSON.parse( xhr.responseText );
20 if( data.status == 'done' ) {
21 progressbar.val( 100 );
22 progressbar.next('.fileuploadpercent').text( '100' );
24 callback( data.status, data.fileid, data.errors );
26 xhr.onerror = function (e) {
27 // Probably only fires for network failure
28 alert('An error occurred while uploading.');