Множественная загрузка
Как поставить задержку между каждым POST?,
Понимаете на сервере записывается сессия получая данные из поста,
причем в массив, но посты как то передаются хаотично
из за асинхронности так что не успевает в сессию попсть дата из первого поста как туда записываются данные из третьего,
нужно поставить условие поочередности отправления постов
var filename = item.replaceName || item.file.name;
xhr.open("POST", url);
xhr.setRequestHeader('X-CSRF-TOKEN', $('meta[name="csrf-token"]').attr('content'));
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
if ($.support.fileSending) {
/*alert('item_file'+item.file)*/
// W3C (Chrome, Safari, Firefox 4+)
var formData = new FormData();
formData.append((item.fieldName || 'file'), item.file);
xhr.send(formData);
} else if ($.support.fileReading && xhr.sendAsBinary) {
// firefox < 4
var boundary = "xxxxxxxxx";
xhr.setRequestHeader("Content-Type", "multipart/form-data, boundary="+boundary);
xhr.setRequestHeader("Cache-Control", "no-cache");
var body = "--" + boundary + "\r\n";
filename = unescape(encodeURIComponent(filename));
body += "Content-Disposition: form-data; name='"+(item.fieldName || 'file')+"'; filename='" + filename + "'\r\n";
body += "Content-Type: application/octet-stream\r\n\r\n";
body += (item.file.getAsBinary ? item.file.getAsBinary() : item.file.readAsBinary()) + "\r\n";
body += "--" + boundary + "--";
xhr.sendAsBinary(body);
} else {
// Other
xhr.setRequestHeader('Upload-Filename', item.file.name);
xhr.setRequestHeader('Upload-Size', item.file.size);
xhr.setRequestHeader('Upload-Type', item.file.type);
xhr.send(item.file);
}
item.xhr = xhr;
}
/* binding callbacks */
var isFileField = ((self.get(0).tagName == 'INPUT') && (this.attr('type') == 'file'));
if (isFileField) {
var myName = self.eq(0).attr('name');
if (!$.support.fileSelecting) {
if (myName.charAt(myName.length-1) != ']') {
myName += '[]';
}
self.attr('name', myName);
self.attr('multiple', false);
var action = self.parents('form').attr('action');
self._damnUploaderFakeForm = $('<form/>').attr({
method: 'post',
enctype: 'multipart/form-data',
action: action
}).hide().appendTo('body');
} else {
self.attr('multiple', true);
}
self._damnUploaderChangeCallback = function() {
self._damnUploaderFilesAddMap($.support.fileSelecting ? this.files : this, set.onSelect);
};
self.on({
change: self._damnUploaderChangeCallback
});
}
if (set.dropping) {
self.on({
drop: function(e) {
self._damnUploaderFilesAddMap(e.originalEvent.dataTransfer.files, set.onSelect);
return false;
}
});
if (set.dropBox) {
$(set.dropBox).on({
drop: function(e) {
self._damnUploaderFilesAddMap(e.originalEvent.dataTransfer.files, set.onSelect);
return false;
}
});
}
}
return self;
}
////////////////////////////////////////////////////////////////////
// controls
switch(params) {
case 'addItem':
if (!data) {
return false;
}
var queueId = uniq(5);
if (data.file.fake) {
var input = $(data.file.inputElement);
var cloned = $(input).clone();
$(input).before(cloned);
$(input).attr('id', queueId);
$(input).appendTo(self._damnUploaderFakeForm);
cloned.on({
change: self._damnUploaderChangeCallback
});
self._damnUploaderItemsCount++;
return queueId;
}
if (!checkIsFile(data.file)) {
return false;
}
queue[queueId] = data;
self._damnUploaderItemsCount++;
return queueId;
break;
case 'startUpload':
if (!set.url) {
return self;
}
if (!$.support.fileSelecting) {
self._damnUploaderFakeForm.submit();
return self;
}
$.each(queue, function(queueId, item) {
//completed
var compl = item.onComplete;
item.fieldName = item.fieldName || set.fieldName;
item.onComplete = function(successful, data, error) {
if (!this.cancelled) {
delete queue[queueId];
self._damnUploaderItemsCount--;
}
if ($.isFunction(compl)) {
compl.call(this, successful, data, error);
// alert(self._damnUploaderSettings.onAllComplete);
}
if ((self._damnUploaderItemsCount == 0) && ($.isFunction(set.onAllComplete))) {
set.onAllComplete.call(self,data);
}
};
self._damnUploaderUploadItem(set.url, item)
});
break;
case 'itemsCount':
return self._damnUploaderItemsCount;
break;
case 'cancelAll':
if (!$.support.fileSelecting) {
self._damnUploaderItemsCount = 0;
self._damnUploaderFakeForm.empty();
return self;
}
$.each(queue, function(key, item) {
self.damnUploader('cancel', key);
});
break;
case 'cancel':
var queueId = data.toString();
if (self._damnUploaderItemsCount > 0) {
if (!$.support.fileSelecting) {
var removingItem = $('#'+queueId);
if (removingItem.length > 0) {
removingItem.remove();
self._damnUploaderItemsCount--;
}
return self;
}
if (queue[queueId] !== undefined) {
if (queue[queueId].xhr) {
queue[queueId].cancelled = true;
queue[queueId].xhr.abort();
}
delete queue[queueId];
self._damnUploaderItemsCount--;
}
}
break;
case 'setParam':
var acceptParams = ['url', 'multiple', 'fieldName', 'limit'];
self._damnUploaderSettings['url'] = data.url;
self._damnUploaderSettings['onAllComplete'] = data.onAllComplete;
break;
}
return self;
};
})(window.jQuery);