diff --git a/site/templates/upload.html b/site/templates/upload.html index e952d53..04f10e6 100644 --- a/site/templates/upload.html +++ b/site/templates/upload.html @@ -59,7 +59,7 @@
Nubes - Сверка договоров v1.34 + Сверка договоров v1.35
@@ -155,53 +155,47 @@ window.removeFile = removeFile; - // ── Чанковая загрузка base64 (10KB) ────────────────────── + // ── Загрузка base64 (JSON, с повторами) ────────────────── - function uploadFileChunked10k(file, cid, onProgress) { - return new Promise(function(resolve, reject) { - var reader = new FileReader(); - reader.onload = function() { - var b64 = reader.result.split(',')[1]; - var CHUNK = 10 * 1024; - var totalChunks = Math.ceil(b64.length / CHUNK); - var uploadId = file.name + '_' + Date.now() + '_' + Math.random().toString(36).substr(2, 6); - var done = 0; - - function sendChunk(i) { - if (i >= totalChunks) { resolve({contract_id: cid}); return; } - var piece = b64.substring(i * CHUNK, (i + 1) * CHUNK); + function uploadFileJSON(file, cid, onProgress) { + var b64 = null; + + function tryUpload(attempt) { + return new Promise(function(resolve, reject) { + function doSend() { var xhr = new XMLHttpRequest(); - xhr.open('POST', '/chunk_json'); + xhr.open('POST', '/upload'); xhr.setRequestHeader('Content-Type', 'application/json'); - xhr.timeout = 30000; + xhr.timeout = 120000; + xhr.upload.onprogress = function(e) { + if (e.lengthComputable && onProgress) onProgress(Math.round(e.loaded / e.total * 100)); + }; xhr.onload = function() { if (xhr.status === 200) { - try { - var resp = JSON.parse(xhr.responseText); - if (resp.error) { reject(new Error(resp.error)); return; } - if (resp.contract_id) cid = resp.contract_id; - done++; - if (onProgress) onProgress(Math.round(done / totalChunks * 100)); - sendChunk(i + 1); - } catch(e) { reject(new Error('Bad JSON')); } + try { resolve(JSON.parse(xhr.responseText)); } + catch(e) { reject(new Error('Bad JSON')); } } else { reject(new Error('HTTP ' + xhr.status)); } }; xhr.onerror = function() { reject(new Error('Сеть')); }; xhr.ontimeout = function() { reject(new Error('Таймаут')); }; - xhr.send(JSON.stringify({ - upload_id: uploadId, - filename: file.name, - chunk_index: i, - total_chunks: totalChunks, - data: piece, - cid: cid || null - })); + xhr.send(JSON.stringify({filename: file.name, data: b64, cid: cid || null})); } - sendChunk(0); - }; - reader.onerror = function() { reject(new Error('Read error')); }; - reader.readAsDataURL(file); - }); + + if (b64) { doSend(); return; } + var reader = new FileReader(); + reader.onload = function() { b64 = reader.result.split(',')[1]; doSend(); }; + reader.onerror = function() { reject(new Error('Read error')); }; + reader.readAsDataURL(file); + }).catch(function(err) { + if (attempt < 3) { + return new Promise(function(r) { setTimeout(r, 1000); }).then(function() { + return tryUpload(attempt + 1); + }); + } + throw err; + }); + } + return tryUpload(1); } // ── Выбор файла → сразу загрузка ──────────────────────────── @@ -225,7 +219,7 @@ var startTime = Date.now(); try { - var resp = await uploadFileChunked10k(f, contractId, function(pct) { + var resp = await uploadFileJSON(f, contractId, function(pct) { fileQueue[rowIdx].status = '↑ ' + pct + '%'; renderTable(); });