Validate file upload with html5 file api

By | February 14, 2018

The html5 file api provides lots of new features which can be used to read files on clientside from within the browser.

Lets take a look at this Demo first :

This file upload control will only take zip or png files which are less than 1MB in size. For any other files it will show an error message.

And this functionality if purely clientside. No server interaction or uploads yet.



<input type="file" data-file_type="zip|png" data-max_size="1000000">

As we can see above , the validation parameters are stored inside the input tag itself using custom data tags. So allowed file types are zip and png , and allowed maximum size is 1000000 bytes , that is nearly 1 MB.


Data attributes are used to specify the allowed file types and the maximum size of the upload file. The extensions are used to map to a list of mime types (later in this article).

	if(typeof $(this).attr('data-file_type') == 'string')
		var file_types = $(this).attr('data-file_type').split('|');
	var mimes = get_mimes(file_types);
	var max_size = parseInt($(this).attr('data-max_size'));
		var finput = $(this);
		var files =; // FileList object

		// files is a FileList of File objects. List some properties.
		var output = [];
		for (var i = 0, f; f = files[i]; i++) 
			//Check mime type
			if(jQuery.inArray(f.type , mimes) == -1)
				alert('File type '+ f.type + ' not allowed');
			//Check size
			else if(f.size > max_size)
				alert('Maximum file size is ' + max_size + ' bytes.');
			//Validation ok
				output.push('<strong>',, '</strong> (', f.type || 'n/a', ') - ', f.size, ' bytes, last modified: ', f.lastModifiedDate.toLocaleDateString() );
		finput.after('<div>' + output.join('') + '</div>');

The above code uses jquery.

In html5 capable browsers when a file is selected for upload in a file input , then the input element has a Filelist object in its files attribute which is an object of type FileList (which is a Collection of File objects).

Information about each file can be retrieved like this :

var files =; // FileList object
var file_count = files.length;

var file_1 = files[0]; // or files.item(0);

var name =;
var size = file_1.size; 
var type = file_1.type;
var lastModifiedDate = file_1.lastModifiedDate;

The things to check are file type and file size. That is all that can be checked.
The type variable contains the mime type of the file. So we have an existing list of mime types useful for the purpose.

List of mimes

var mime_types = {

The above list of mime types can give the mime type of a give extension like zip or png. Next we have a function that provides the list of allowed mimes as an array :

	Get the mimes of a list of extensions as an array
function get_mimes(extensions)
	var mimes = [];
	for(var i in extensions)
		var ext = extensions[i];
		if(ext in mime_types)
			var mime = mime_types[ext];
				jQuery.merge(mimes , mime);
	return mimes;

This method can be used like this get_mimes(['zip' , 'png']) and it will return an array of mimes for the given extensions. This return values is used in the validation part to simply check if the mime of the selected file is there in this list or not. If not , then show error.

References :

The documentation of the file api lies here :

Details of browser support are available here : api

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

Leave a Reply

Your email address will not be published. Required fields are marked *