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