forked from burt202/lite-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.liteuploader.js
More file actions
130 lines (107 loc) · 2.65 KB
/
Copy pathjquery.liteuploader.js
File metadata and controls
130 lines (107 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* liteUploader v1.4.1 | https://github.com/burt202/lite-uploader | Aaron Burtnyk (http://www.burtdev.net) */
$.fn.liteUploader = function (userOptions)
{
"use strict";
var defaults = {},
options = {};
defaults = {
script: null,
allowedFileTypes: null,
maxSizeInBytes: null,
customParams: {},
before: function() { return true; },
each: function(file, errors) {},
progress: function (percentage) {},
success: function(response) {},
fail: function(jqXHR) {}
};
options = $.extend(defaults, userOptions);
function findErrors (file, options)
{
var errorsArray = [];
if (options.allowedFileTypes && $.inArray(file.type, options.allowedFileTypes.split(',')) === -1)
{
errorsArray.push({'type': 'type', 'rule': options.allowedFileTypes, 'given': file.type});
}
if (options.maxSizeInBytes && file.size > options.maxSizeInBytes)
{
errorsArray.push({'type': 'size', 'rule': options.maxSizeInBytes, 'given': file.size});
}
return errorsArray;
}
function validateFiles (files, options)
{
var errors = false;
$.each(files, function(i)
{
var errorsArray = findErrors(files[i], options);
if (errorsArray.length > 0) { errors = true; }
if (options.each(files[i], errorsArray) === false) { errors = true };
});
if (errors) { return false; }
return true;
}
function collateFormData (obj, customParams, files)
{
var formData = new FormData();
if (obj.attr('id')) { formData.append('liteUploader_id', obj.attr('id')); }
$.each(customParams, function(key, value)
{
formData.append(key, value);
});
$.each(files, function(i)
{
formData.append(obj.attr('name') + '[]', files[i]);
});
return formData;
}
function resetInput (obj)
{
obj.replaceWith(obj.clone(true));
}
function performUpload (obj, options, formData)
{
$.ajax(
{
xhr: function()
{
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', function (evt)
{
if (evt.lengthComputable)
{
options.progress(Math.floor((evt.loaded / evt.total) * 100));
}
}, false);
return xhr;
},
url: options.script,
type: 'POST',
data: formData,
processData: false,
contentType: false
})
.always(function ()
{
resetInput(obj);
})
.done(function(response)
{
options.success(response);
})
.fail(function(jqXHR)
{
options.fail(jqXHR);
});
}
this.change(function ()
{
var obj = $(this);
if (!obj.attr('name') || !options.script || !options.before() || this.files.length === 0 || !validateFiles(this.files, options))
{
resetInput(obj);
return;
}
performUpload(obj, options, collateFormData(obj, options.customParams, this.files));
});
};