upload files one by one

This commit is contained in:
j 2016-01-13 21:47:51 +05:30
parent fcc03c742d
commit cd949c4f20

View file

@ -1012,31 +1012,43 @@ oml.updateDebugMenu = function() {
}; };
oml.supportedExtensions = ['pdf', 'epub', 'cbr', 'cbz']; oml.supportedExtensions = ['pdf', 'epub', 'cbr', 'cbz'];
oml.upload = function(files, callback) {
var request = new XMLHttpRequest(), oml.upload = function(fileslist, callback) {
url = '/api/upload/'; var files = [], ids = [];
request.onreadystatechange = function() { for (var i = 0; i < fileslist.length; i++) {
if (request.readyState == 4) { var extension = Ox.last(fileslist[i].name.split('.'));
if (request.status == 200) {
callback(JSON.parse(request.responseText), null);
} else {
callback(null, {
code: request.status,
text: request.statusText
});
}
}
};
var formData = new FormData();
for (var i=0; i < files.length; i++) {
var extension = Ox.last(files[i].name.split('.'));
if (Ox.contains(oml.supportedExtensions, extension)) { if (Ox.contains(oml.supportedExtensions, extension)) {
formData.append('files', files[i]); files.push(fileslist[i]);
} }
} }
if (oml.user.ui._list[0] == ':' && oml.user.ui._list.length > 1) { Ox.serialForEach(files, function(file, index, array, next) {
formData.append('list', oml.user.ui._list.slice(1)); var request = new XMLHttpRequest(),
} url = '/api/upload/';
request.open('post', url, true); request.onreadystatechange = function() {
request.send(formData); if (request.readyState == 4) {
if (request.status == 200) {
next();
var response = JSON.parse(request.responseText);
ids.concat(response.data.ids);
} else {
callback(null, {
code: request.status,
text: request.statusText
});
}
}
};
var formData = new FormData();
formData.append('files', file);
if (oml.user.ui._list[0] == ':' && oml.user.ui._list.length > 1) {
formData.append('list', oml.user.ui._list.slice(1));
}
request.open('post', url, true);
request.send(formData);
}, function() {
callback({
'status': {'code': 200},
'data': {'ids': ids}
});
});
}; };