A jquery plugin for ajax file upload.
- jquery
Install from npm:
$ npm install @kanety/jquery-simple-upload --save
Build file input field:
<input type="file" name="file" multiple="true" id="input">
Then run:
$('#input').simpleUpload({
url: 'YOUR_BACKEND_URL',
method: 'post'
});
Add drop zone and progress message:
<div id="drop_zone"></div>
<div id="progress"></div>
$('#input').simpleUpload({
...
dropZone: '#drop_zone',
progress: '#progress'
});
Set additional ajax options:
$('#input').simpleUpload({
...
ajax: {
headers: {
'HEADER_KEY': 'HEADER_VALUE'
},
dataType: 'application/json',
timeout: 0,
async: true
}
});
Set additional query parameters:
$('#input').simpleUpload({
// set object
params: {
'KEY': 'VALUE'
},
// or set callback
params: function() {
return { 'KEY': 'VALUE' };
},
});
$('#input').simpleUpload({
...
maxFileNum: 4,
maxFileSize: 10 * 1024 * 1024, // Bytes
allowedFileName: /\.txt$/,
allowedMimeType: /^text\//
}).on('upload:over', function(e, files) {
...
}).on('upload:invalid', function(e, files) {
// files[i].reason contains the rejected reason
...
});
$('#input').simpleUpload({
...
}).on('upload:before', function(e, files) {
...
}).on('upload:after', function(e, files) {
...
}).on('upload:start', function(e, file, i) {
...
}).on('upload:progress', function(e, file, i, loaded, total) {
...
}).on('upload:end', function(e, file, i) {
...
}).on('upload:done', function(e, file, i) {
...
}).on('upload:fail', function(e, file, i) {
...
});
The library is available as open source under the terms of the MIT License.