-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ajax file upload during job submission.
Closes #383
- Loading branch information
1 parent
36a3aad
commit dcad12b
Showing
12 changed files
with
1,903 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
jQuery(function($) { | ||
$('.wp-job-manager-file-upload').fileupload({ | ||
dataType: 'json', | ||
url: job_manager_ajax_file_upload.ajax_url, | ||
maxNumberOfFiles: 1, | ||
formData: { | ||
script: true, | ||
action: 'job_manager_upload_file' | ||
}, | ||
add: function (e, data) { | ||
var $file_field = $( this ); | ||
var $form = $file_field.closest( 'form' ); | ||
var $uploaded_files = $file_field.parent().find('.job-manager-uploaded-files'); | ||
var uploadErrors = []; | ||
|
||
// Validate type | ||
var allowed_types = $(this).data('file_types'); | ||
|
||
if ( allowed_types ) { | ||
var acceptFileTypes = new RegExp( "(\.|\/)(" + allowed_types + ")$", "i" ); | ||
|
||
if ( data.originalFiles[0]['name'].length && ! acceptFileTypes.test( data.originalFiles[0]['name'] ) ) { | ||
uploadErrors.push( job_manager_ajax_file_upload.i18n_invalid_file_type + ' ' + allowed_types ); | ||
} | ||
} | ||
|
||
if ( uploadErrors.length > 0 ) { | ||
alert( uploadErrors.join( "\n" ) ); | ||
} else { | ||
$form.find(':input[type="submit"]').attr( 'disabled', 'disabled' ); | ||
data.context = $('<progress value="" max="100"></progress>').appendTo( $uploaded_files ); | ||
data.submit(); | ||
} | ||
}, | ||
progress: function (e, data) { | ||
var $file_field = $( this ); | ||
var $uploaded_files = $file_field.parent().find('.job-manager-uploaded-files'); | ||
var progress = parseInt(data.loaded / data.total * 100, 10); | ||
data.context.val( progress ); | ||
}, | ||
done: function (e, data) { | ||
var $file_field = $( this ); | ||
var $form = $file_field.closest( 'form' ); | ||
var $uploaded_files = $file_field.parent().find('.job-manager-uploaded-files'); | ||
var multiple = $file_field.attr( 'multiple' ) ? 1 : 0; | ||
var image_types = [ 'jpg', 'gif', 'png', 'jpeg', 'jpe' ]; | ||
|
||
data.context.remove(); | ||
|
||
$.each(data.result.files, function(index, file) { | ||
if ( file.error ) { | ||
alert( file.error ); | ||
} else { | ||
if ( $.inArray( file.extension, image_types ) >= 0 ) { | ||
var html = $.parseHTML( job_manager_ajax_file_upload.js_field_html_img ); | ||
$( html ).find('.job-manager-uploaded-file-preview img').attr( 'src', file.url ); | ||
} else { | ||
var html = $.parseHTML( job_manager_ajax_file_upload.js_field_html ); | ||
$( html ).find('.job-manager-uploaded-file-name code').text( file.name ); | ||
} | ||
|
||
$( html ).find('.input-text').val( file.url ); | ||
$( html ).find('.input-text').attr( 'name', 'current_' + $file_field.attr( 'name' ) ); | ||
|
||
if ( multiple ) { | ||
$uploaded_files.append( html ); | ||
} else { | ||
$uploaded_files.html( html ); | ||
} | ||
} | ||
}); | ||
|
||
$form.find(':input[type="submit"]').removeAttr( 'disabled' ); | ||
} | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
dcad12b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fails when the field is set to allow multiple uploads. I get the mime type error even if I upload the same image as the other image field. Works when multiple => true is removed.
dcad12b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have confirmed the issue above from @spencerfinnell as well