forked from goker-dev/droparea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
droparea.js
executable file
·126 lines (117 loc) · 4.51 KB
/
droparea.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
(function( $ ){
var s;
// Methods
var m = {
init: function(e){},
start: function(e){},
complete: function(r){},
error: function(r){ alert(r.error); return false; },
traverse: function(files,area){
if (typeof files !== "undefined") {
for (var i=0, l=files.length; i<l; i++) {
m.upload(files[i], area);
}
} else {
area.html(nosupport);
}
},
upload: function(file, area){
//area.empty();
var progress = $('<div>',{
'class':'progress'
});
area.append(progress);
// File type control
if (typeof FileReader === "undefined" || !(/image/i).test(file.type)) {
//area.html(file.type,s.noimage);
alert('only image files: jpeg, png, gif');
return false;
}
// File size control
if (file.size > (s.maxsize * 1024)) {
//area.html(file.type,s.maxsize);
alert('max upload size: ' + s.maxsize + 'Kb');
return false;
}
// Uploading - for Firefox, Google Chrome and Safari
var xhr = new XMLHttpRequest();
// Update progress bar
xhr.upload.addEventListener("progress", function (e) {
if (e.lengthComputable) {
var loaded = Math.ceil((e.loaded / e.total) * 100) + "%";
progress.css({
'height':loaded
}).html(loaded);
}
}, false);
// File uploaded
xhr.addEventListener("load", function (e) {
var r = jQuery.parseJSON(e.target.responseText);
s.complete(r);
area.find('img').remove();
area.data('value',r.filename)
.append($('<img>',{'src': r.path + r.filename + '?' + Math.random()}));
progress.addClass('uploaded');
progress.html(s.uploaded).fadeOut('slow');
}, false);
xhr.open("post", s.post, true);
// Set appropriate headers
xhr.setRequestHeader("x-file-name", file.fileName);
xhr.setRequestHeader("x-file-size", file.fileSize);
xhr.setRequestHeader("x-file-type", file.type);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
// Set request headers
for (var i in area.data())
if (typeof area.data(i) !== "object")
xhr.setRequestHeader('x-param-'+i, area.data(i));
var fd = new FormData();
fd.append("x-file-name", file);
xhr.send(fd);
}
};
$.fn.droparea = function(o) {
// Settings
s = {
'init': m.init,
'start': m.start,
'complete': m.complete,
'instructions': 'drop an image file here',
'over' : 'drop file here!',
'nosupport' : 'No support for the File API in this web browser',
'noimage' : 'Unsupported file type!',
'uploaded' : 'Uploaded',
'maxsize' : '500', //Kb
'post' : 'upload.php'
};
this.each(function(){
if(o) $.extend(s, o);
var instructions = $('<div>').appendTo($(this));
s.init($(this));
if(!$(this).data('value'))
instructions.addClass('instructions').html(s.instructions);
$(this)
.bind({
dragleave: function (e) {
e.preventDefault();
if($(this).data('value'))
instructions.removeClass().empty();
else
instructions.removeClass('over').html(s.instructions);
},
dragenter: function (e) {
e.preventDefault();
instructions.addClass('instructions over').html(s.over);
},
dragover: function (e) {
e.preventDefault();
}
});
this.addEventListener("drop", function (e) {
e.preventDefault();
s.start($(this));
m.traverse(e.dataTransfer.files, $(this));
instructions.removeClass().empty();
},false);
});
};
})( jQuery );