-
Notifications
You must be signed in to change notification settings - Fork 42
/
jquery-fileupload.js
182 lines (158 loc) · 5.28 KB
/
jquery-fileupload.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* fileUpload
* http://abandon.ie
*
* Plugin to add file uploads to jQuery ajax form submit
*
* November 2013
*
* @version 0.9
* @author Abban Dunne http://abandon.ie
* @license MIT
*
*/
;(function($, window, document, undefined)
{
// Create the defaults once
var pluginName = "fileUpload",
defaults = {
uploadData : {},
submitData : {},
uploadOptions : {},
submitOptions : {},
before : function(){},
beforeSubmit : function(){ return true; },
success : function(){},
error : function(){},
complete : function(){}
};
// The actual plugin constructor
function Plugin(element, options)
{
this.element = element;
this.$form = $(element);
this.$uploaders = $('input[type=file]', this.element);
this.files = {};
this.settings = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
Plugin.prototype =
{
/**
* Initialize the plugin
*
* @return void
*/
init: function()
{
this.$uploaders.on('change', { context : this }, this.processFiles);
this.$form.on('submit', { context : this }, this.uploadFiles);
},
/**
* Process files after they are added
*
* @param jQuery event
* @return void
*/
processFiles: function(event)
{
var self = event.data.context;
self.files[$(event.target).attr('name')] = event.target.files;
},
/**
* Handles the file uploads
*
* @param jQuery event
* @return void
*/
uploadFiles: function(event)
{
event.stopPropagation(); // Stop stuff happening
event.preventDefault(); // Totally stop stuff happening
var self = event.data.context;
// Run the before callback
self.settings.before();
// Declare a form data object
var data = new FormData();
data.append('file_upload_incoming', '1');
// Add the files
$.each(self.files, function(key, field)
{
$.each(field, function(key, value)
{
data.append(key, value);
});
});
// If there is uploadData passed append it
$.each(self.settings.uploadData, function(key, value)
{
data.append(key, value);
});
// Perform Ajax call
$.ajax($.extend({}, {
url: self.$form.attr('action'),
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false, // Don't process the files, we're using FormData
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
success: function(data, textStatus, jqXHR){ self.processSubmit(event, data); },
error: function(jqXHR, textStatus, errorThrown){ self.settings.error(jqXHR, textStatus, errorThrown); }
}, self.settings.uploadOptions));
},
/**
* Submits form data with files
*
* @param jQuery event
* @param object
* @return void
*/
processSubmit: function(event, uploadData)
{
var self = event.data.context;
// Run the beforeSubmit callback
if(!self.settings.beforeSubmit(uploadData)) return;
// Serialize the form data
var data = self.$form.serializeArray();
// Loop through the returned array from the server and add it to the new POST
$.each(uploadData, function(key, value)
{
data.push({
'name' : key,
'value' : value
});
});
// If there is uploadData passed append it
$.each(self.settings.submitData, function(key, value)
{
data.push({
'name' : key,
'value' : value
});
});
$.ajax($.extend({}, {
url: self.$form.attr('action'),
type: 'POST',
data: data,
cache: false,
dataType: 'json',
success: function(data, textStatus, jqXHR){ self.settings.success(data, textStatus, jqXHR); },
error: function(jqXHR, textStatus, errorThrown){ self.settings.error(jqXHR, textStatus, errorThrown); },
complete: function(jqXHR, textStatus){ self.settings.complete(jqXHR, textStatus); }
}, self.settings.submitOptions));
}
};
$.fn[pluginName] = function(options)
{
return this.each(function()
{
if(!$.data(this, "plugin_" + pluginName))
{
$.data(this, "plugin_" + pluginName, new Plugin(this, options));
}
});
};
})(jQuery, window, document);