
A tiny, dependency-free, highly customizable and configurable, easy to use file input with some pretty sweet features.
- Tiny (JS ~9kb + CSS ~1kb = ~10kb total minified [add an additional ~4kb if using the theme])
- Zero dependencies
- Wide Browser Support
- Use as little (or as much) HTML markup as you want
- Optional default CSS styling (which looks really cool)
- Drag-and-drop
- Append files to the input
- Remove individual files from the input
- Drag files out of the box to remove from the input
- Drag files between multiple
filebokz
elements - Files are all managed with the
file
input, so they are submitted with your form and AJAX isn't required (however you can definitely use it if you wish) - Preview images before they are uploaded to your server (and specify images or HTML content to display for files that aren't images)
- Specify allowed file extensions, max number of files, max individual file size, and max total file size, as well as the error messages that are displayed when those rules are broken (and specify images or HTML content to display for files that aren't images)
- Pretty much everything is configurable and flexible
Visit https://filebokz.js.org
Download the latest version of FileBokz and then place the following HTML in your page's head element:
<script type="text/javascript" src="dist/filebokz.min.js"></script>
<link rel="stylesheet" href="dist/filebokz.min.css" />
<!--Optional theme-->
<link rel="stylesheet" href="dist/filebokz-theme.min.css" />
CDN (Courtesy of jsDelivr)
Place the following HTML in your page's head element (check to make sure the version in the URLs is the version you want):
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/fivefifteen/filebokz@0.2/dist/filebokz.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fivefifteen/filebokz@0.2/dist/filebokz.min.css" />
<!--Optional theme-->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fivefifteen/filebokz@0.2/dist/filebokz-theme.min.css" />
npm install filebokz --save
// ES6
import filebokz from 'filebokz'
// CommonJS
const filebokz = require('filebokz')
fetcher install fivefifteen/filebokz --save
bower install fivefifteen/filebokz --save
<div class="filebokz">
<input type="file" name="my-file" />
<label for="my-file">
<strong class="no-files">Choose a file</strong>
<span class="no-files is-draggable">or drag it here</span>
<span class="files"></span>
<span class="error-msg"></span>
</label>
</div>
Initiates FileBokz.
- elements - The elements to target. Accepts a string to pass into
document.querySelectorAll()
, aNodeList
instance, or anHTMLElement
instance. (Defaults to.filebokz
) - applyClass - A class name to apply to all matching elements. Set to
false
to not apply any class. (Defaults tofilebokz
)
window.addEventListener('load', function () {
filebokz()
filebokz('.custom-filebokz', false)
filebokz(document.getElementById('my-filebokz'))
filebokz(document.getElementsByClassName('file-upload'))
})
Use these classes on elements inside of the filebokz
element to customize your input.
Elements with this class will be hidden unless the root filebokz
element has this class which is added if any errors occur.
Elements with this class will have their contents replaced with any errors that occur.
If any elements have the files
class, elements are appended to them with this class for displaying file names and/or previews when files are added to the input.
Elements with this class will have their contents replaced with the number of files in the input.
Elements with this class will have elements appended to them with the file
class for displaying file names and/or previews when files are added to the input.
If displaying file previews, apply this class to any elements with the files
class for fancier styling (when filebokz-theme.css
is used).
Elements with this class will be hidden unless the root filebokz
element has this class which is added if the input has files.
Elements with this class will be hidden unless the root filebokz
element has this class which is added if the input is in focus.
Elements with this class will be hidden unless the root filebokz
element has this class which is added if the browser supports advanced features.
Elements with this class will be hidden unless the root filebokz
element has this class which is added if the browser supports drag-and-drop.
Elements with this class will be hidden unless the root filebokz
element has this class which is added if a file is being dragged over it.
Elements with this class will be hidden unless the root filebokz
element has this class which is added if a file is being dragged out of the file box and being removed.
Elements with this class will be hidden unless the root filebokz
element has this class which is added if a file is being dragged out of the file box and being moved to another filebokz
element.
Elements with this class will be hidden unless the root filebokz
element has this class which is added after the JavaScript has been ran (thus meaning the browser supports JavaScript).
Elements with this class will be hidden if the root filebokz
element has the class has-files
which is added if the input has files.
Add this class to your root filebokz
element if you do not wish for the JavaScript to do anything with it (mainly used for demo purposes).
Elements with this class will be hidden if the root filebokz
element has the in-focus
class which is added if the input is in focus.
Elements with this class will be hidden if the root filebokz
element has the is-advanced
class which is added if the browser supports advanced features.
Elements with this class will be hidden if the root filebokz
element has the is-draggable
class which is added if the browser supports drag-and-drop.
Elements with this class will be hidden if the root filebokz
element has the is-dragging
class which is added if a file is being dragged over it.
Elements with this class will be hidden if the root filebokz
element has the is-removing
class which is added if a file is being dragged out of the file box and being removed.
Elements with this class will be hidden if the root filebokz
element has the is-transferring
class which is added if a file is being dragged out of the file box and being moved to another filebokz
element.
Add this class to an element inside of the files
element's content
, content-before
, or content-after
data attributes so that when the element is clicked, it removes the file from the input.
Elements with this class will have their contents replaced with the combined file size of all files in the input.
Apply these attributes with the data-
prefix to your filebokz
element to configure your input. (ex. data-max-file-size="5120"
)
A comma seperated (no spaces) list of allowed file extensions. Defaults to allowing any file extension. Add [none]
to this list to allow files without extensions.
The error message that is displayed when a user tries to upload a file that isn't in the allowed-extensions
list. Any instances of {variable}
will be replaced with the list of allowed file extensions. (Defaults to Only the following file types are allowed: {variable}.
)
Set this to false
to replace the files in the input when a file is selected rather than adding it. (Defaults to true
)
Set this to true
to automatically submit the parent form when a file is uploaded. (Defaults to false
)
The number of milliseconds after the error-display-duration
before clearing out the contents of the error-msg
element after an error has occurred. (Defaults to 250
)
The number of milliseconds before removing the error
class from the filebokz
element after an error has occurred. (Defaults to 3000
)
The maximum number of files that are allowed to be uploaded. This is only applied if the input has the multiple
attribute. (Defaults to null
[no limit])
The error message that is displayed when a user tries to upload more files than max-files
allows. Instances of {variable}
are replaced with the value of max-files
and instances of {s}
are replaced with an "s" if that number is more than 1, otherwise it is replaced with nothing. (Defaults to A maximum of {variable} file{s} can be uploaded.
)
The maximum size of a single file that is allowed to be uploaded. (Defaults to null
[no limit])
The error message that is displayed when a user tries to upload a file that is bigger than what max-file-size
allows. Instances of {variable}
are replaced with the value of max-file-size
. (Defaults to File size cannot exceed {variable}.
)
The maximum size of all files combined that is allowed to be uploaded. (Defaults to null
[no limit])
The error message that is displayed when a user tries to upload files whose combined file size is bigger than what max-size
allows. (Defaults to Total combined file size cannot exceed {variable}.
)
Apply these attributes with the data-
prefix to any element with the files
class inside of your filebokz
element to customize how each file is displayed.
These attributes (with the exception of draggable
) can have a MIME type appended to them with any +
or /
symbols swapped out for -
to customize how a specific file type is displayed (ex. content-before-image
or even more specifically: content-before-image-svg-xml
).
These attributes (with the exception of draggable
and element
) will have the following replaced with information about each file:
{name}
- The file name.{size}
- The file size (in a human readable format).{type}
- The MIME type of the file.{url}
- If the file is an image, this will be a temporary URL to the file for previewing, otherwise it will be the contents of thedata-url
attribute, and if that doesn't exist, this will be blank.
The content that is placed inside of the file
element. (Defaults to {name}
)
The content that is prepended to content
. (Defaults to null
[blank])
The content that is appended to content
. (Defaults to null
[blank])
Set this to false
to disable the dragging of file
elements. (Defaults to true
)
The type of HTML element that the file
element should be. (Defaults to span
)
The URL of the file preview. This is what {url}
is set to in the content
, content-before
, and content-after
attributes. (Defaults to null
[blank] unless the file is an image, in which case it is set to a temporary URL of the file for previewing.)
The file-added
, file-removed
, and error
events are fired to the containing filebokz
element when described events happen.
var fileElement = document.querySelector('.filebokz')
fileElement.addEventListener('file-added', function (e) {
console.log('A file was added')
})
fileElement.addEventListener('file-removed', function (e) {
console.log('A file was removed')
})
fileElement.addEventListener('error', function (e) {
console.log('An error occured', e.errorType, e.errorMessage)
})
-
ColorTap - A tiny, dependency-free, color input field helper that utilizes the native color picker.
-
GrowField - A tiny, dependency-free JavaScript module for making textarea elements grow with their content.
-
HashJump - A tiny, dependency-free JavaScript module for handling anchor links and scrolling elements into view.
-
Kloner - A tiny, dependency-free JavaScript module for cloning/repeating elements.
MIT. See the license file for more info.