diff --git a/index.cfm b/index.cfm index 71f12c5..fd14637 100644 --- a/index.cfm +++ b/index.cfm @@ -64,7 +64,7 @@
Nubes - Сверка договоров v1.0.33 — Lucee + Сверка договоров v1.0.34 — Lucee
@@ -221,7 +221,7 @@ fileInput.addEventListener('change', function() { renderTable(); }); -// ── Загрузка (читаем все → шлём одним JSON) ──────────── +// ── Загрузка через iframe (отдельный контекст на каждый файл) ─ uploadBtn.addEventListener('click', function() { if (fileQueue.length === 0) return; uploadBtn.disabled = true; @@ -229,77 +229,81 @@ uploadBtn.addEventListener('click', function() { uploadTimer.style.display = 'block'; var pending = fileQueue.filter(function(f) { return !f.doc_id; }); - var payloads = []; - var readIdx = 0; + var done = 0; - function readAll() { - if (readIdx >= pending.length) { sendAll(); return; } - var f = pending[readIdx]; - f.status = 'читаю...'; - renderTable(); - var reader = new FileReader(); - reader.onload = function(e) { - payloads.push({filename: f.name, data: e.target.result.split(',')[1]}); - readIdx++; - uploadTimer.textContent = 'Чтение ' + readIdx + '/' + pending.length; - readAll(); - }; - reader.onerror = function() { - f.status = 'error: чтение'; - renderTable(); - readIdx++; - readAll(); - }; - reader.readAsDataURL(f.file); - } - - function sendAll() { - if (payloads.length === 0) { + function uploadNext(i) { + if (i >= pending.length) { uploadTimer.style.display = 'none'; uploadBtn.disabled = false; + if (contractId && done === pending.length) { + document.getElementById('actionsCard').style.display = 'block'; + document.getElementById('chatCard').style.display = 'block'; + } return; } - uploadTimer.textContent = 'Отправка...'; - var body = {files: payloads}; - if (contractId) body.contract_id = contractId; + var f = pending[i]; + f.status = '0%'; + renderTable(); + uploadTimer.textContent = 'Загрузка ' + (i+1) + '/' + pending.length; - fetch('/upload.cfm', { - method: 'POST', - headers: {'Content-Type': 'application/json'}, - body: JSON.stringify(body) - }).then(function(r) { return r.json(); }) - .then(function(data) { - uploadTimer.style.display = 'none'; - uploadBtn.disabled = false; - if (data.OK) { - contractId = data.CONTRACT_ID || contractId; - for (var i = 0; i < pending.length; i++) { - pending[i].status = 'uploaded'; - } + // Создаём уникальный iframe + var iframeId = 'up_iframe_' + Date.now() + '_' + i; + var iframe = document.createElement('iframe'); + iframe.name = iframeId; + iframe.style.display = 'none'; + iframe.onload = function() { + try { + var resp = iframe.contentDocument.body.textContent; + var r = JSON.parse(resp); + if (r.OK) { + contractId = r.CONTRACT_ID; + f.doc_id = r.DOC_ID; + f.status = 'uploaded'; + done++; var elapsed = ((Date.now() - uploadStartTime) / 1000).toFixed(1); - log('✅ ' + pending.length + ' файлов — ' + elapsed + 'с'); + log('✅ ' + f.name + ' (' + formatSize(f.size) + ') — ' + elapsed + 'с'); } else { - for (var i = 0; i < pending.length; i++) { - pending[i].status = 'error: ' + (data.ERROR || 'неизв.'); - } - log('❌ ' + (data.ERROR || 'неизв.')); + f.status = 'error: ' + (r.ERROR || ''); + log('❌ ' + f.name + ': ' + (r.ERROR || '')); } - renderTable(); - if (contractId) { - document.getElementById('actionsCard').style.display = 'block'; - document.getElementById('chatCard').style.display = 'block'; - } - }).catch(function(e) { - uploadTimer.style.display = 'none'; - uploadBtn.disabled = false; - for (var i = 0; i < pending.length; i++) { - pending[i].status = 'error: ' + e.message; - } - renderTable(); - }); + } catch(e) { + f.status = 'error: ' + e.message; + log('❌ ' + f.name + ': ' + e.message); + } + renderTable(); + document.body.removeChild(iframe); + uploadNext(i + 1); + }; + document.body.appendChild(iframe); + + // Форма внутри iframe + var form = iframe.contentDocument.createElement('form'); + form.method = 'POST'; + form.action = '/upload.cfm'; + form.enctype = 'multipart/form-data'; + + var input = iframe.contentDocument.createElement('input'); + input.type = 'file'; + input.name = 'files'; + // Копируем файл в форму через DataTransfer + var dt = new DataTransfer(); + dt.items.add(f.file); + input.files = dt.files; + form.appendChild(input); + + if (contractId) { + var cidInput = iframe.contentDocument.createElement('input'); + cidInput.type = 'hidden'; + cidInput.name = 'contract_id'; + cidInput.value = contractId; + form.appendChild(cidInput); + } + + iframe.contentDocument.body.appendChild(form); + form.submit(); } - readAll(); + uploadNext(0); }); // ── Инфо ──────────────────────────────────────────────────