this upload function file var http new XMLHttpRequest if http upload h

 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
this.upload = function(file)
{
var http = new XMLHttpRequest();
if (http.upload && http.upload.addEventListener) {
http.onreadystatechange = this.resultController;
http.upload.addEventListener('error', this.httpError);
http.upload.addEventListener('progress', this.progressMonitor, false);
}
var form = new FormData();
form.append('images_directory', 'public/images/'+$('#info').attr('data-dir'));
form.append('image', file);
http.open('POST', '/admin/image/upload/simple');
http.send(form);
};
this.httpError = function()
{
self.clearDiv();
self.imagediv.append(document.createTextNode('При загрузке произошла ошибка. Перезагрузите страницу'));
};
this.resultController = function() {
if (this.readyState == 4) {
if (this.status == 200) {
var json = $.parseJSON(this.response);
self.imageid = json.image.id;
self.input.attr('value', json.image.id);
self.clearDiv();
self.imageEditor();
} else {
self.httpError();
}
}
};
this.progressMonitor = function(e){
if (e.lengthComputable) {
var progress = ((e.loaded/e.total)*100).toFixed();
$('#uploadLabel').text('Загрузка: '+progress.toString()+'%');
}
};