}
}
+// Implement our own AJAX method that uses the features of XMLHttpRequest2
+// but try to let it have a similar interface to jquery.post
+// The data var needs to be a FormData() object.
+// The callback will be given a single argument, which is the response data
+// of the given type.
+
+function post_xhr2( url, data, cb, type ) {
+ if( !type ) {
+ type = 'json';
+ }
+ var xhr = new XMLHttpRequest();
+ // Set the expected response type
+ if( type === 'data' ) {
+ xhr.responseType = 'blob';
+ } else if( type === 'xml' ) {
+ xhr.responseType = 'document';
+ }
+ // Post the form
+ // Gin up an AJAX settings object
+ $.ajaxSetup({ url: url, type: 'POST' });
+ xhr.open( 'POST', url, true );
+ // Handle the results
+ xhr.onload = function( e ) {
+ // Get the response and parse it
+ // Call the callback with the response, whatever it was
+ var xhrs = e.target;
+ if( xhrs.status > 199 && xhrs.status < 300 ) { // Success
+ var resp;
+ if( type === 'json' ) {
+ resp = $.parseJSON( xhrs.responseText );
+ } else if ( type === 'xml' ) {
+ resp = xhrs.responseXML;
+ } else if ( type === 'text' ) {
+ resp = xhrs.responseText;
+ } else {
+ resp = xhrs.response;
+ }
+ cb( resp );
+ } else {
+ // Trigger the ajaxError...
+ _trigger_ajaxerror( e );
+ }
+ };
+ xhr.onerror = _trigger_ajaxerror;
+ xhr.onabort = _trigger_ajaxerror;
+ xhr.send( data );
+}
+
+function _trigger_ajaxerror( e ) {
+ var xhr = e.target;
+ var thrown = xhr.statusText || 'Request error';
+ jQuery.event.trigger( 'ajaxError', [ xhr, $.ajaxSettings, thrown ]);
+}
+
function upload_new () {
// Serialize the upload form, get the file and attach it to the request,
// POST the lot and handle the response.
var newfile = $('#new_file').get(0).files[0];
var reader = new FileReader();
reader.onload = function( evt ) {
- var formvals = $('#new_tradition').serializeArray();
- var params = { 'file': evt.target.result, 'filename': newfile.name };
- $.each( formvals, function( i, o ) {
- params[o.name] = o.value;
- });
+ var data = new FormData();
+ $.each( $('#new_tradition').serializeArray(), function( i, o ) {
+ data.append( o.name, o.value );
+ });
+ data.append( 'file', newfile );
var upload_url = _get_url([ 'newtradition' ]);
- $.post( upload_url, params, function( ret ) {
+ post_xhr2( upload_url, data, function( ret ) {
if( ret.id ) {
$('#upload-collation-dialog').dialog('close');
refreshDirectory();
$('#upload_status').empty().append(
$('<span>').attr('class', 'error').append( ret.error ) );
}
- });
+ }, 'json' );
};
reader.onerror = function( evt ) {
var err_resp = 'File read error';
// Set the upload button to its correct state based on
// whether a file is loaded
file_selected( $('#new_file').get(0) );
+ $('#upload_status').empty();
}
}).ajaxError( function(event, jqXHR, ajaxSettings, thrownError) {
// Reset button state