Skip to content

Instantly share code, notes, and snippets.

@toniher
Last active August 29, 2015 13:56
Show Gist options
  • Save toniher/9140663 to your computer and use it in GitHub Desktop.
Save toniher/9140663 to your computer and use it in GitHub Desktop.
Bit of jQuery code for keeping suitable filename extension in MediaWiki file upload form. Useful when used with default filename parameter in Semantic Forms http://www.mediawiki.org/wiki/Extension:Semantic_Forms/Defining_forms
/** Make filename readonly, so filename is either the default one defined in the form or the filename in your drive **/
$("#mw-upload-form #wpDestFile").attr('readonly', true);
/** Keep file extension in upload. Default filename is changed according the filename extension uploaded **/
$("input:file").change(function (){
var filename = $('input[type=file]').val().replace(/C:\\fakepath\\/i, '');
var extension = filename.substr( (filename.lastIndexOf('.') +1) );
var subs = "."+extension;
var filenamef = $("#wpDestFile").val();
var extensionf = filenamef.substr( (filenamef.lastIndexOf('.') +1) );
var init = "."+extensionf;
var filenamev = filenamef.replace(init, subs);
$("#wpDestFile").val(filenamev);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment