чанки 30KB + 100мс задержка, порог 64KB обойдён, v1.42
This commit is contained in:
+34
-12
@@ -59,7 +59,7 @@
|
|||||||
<body>
|
<body>
|
||||||
<div class="topbar">
|
<div class="topbar">
|
||||||
<img src="{{ url_for('static', filename='nubes-logo.svg') }}" alt="Nubes">
|
<img src="{{ url_for('static', filename='nubes-logo.svg') }}" alt="Nubes">
|
||||||
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.41</span></span>
|
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.42</span></span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="content">
|
<div class="content">
|
||||||
@@ -155,35 +155,57 @@
|
|||||||
|
|
||||||
window.removeFile = removeFile;
|
window.removeFile = removeFile;
|
||||||
|
|
||||||
// ── Загрузка base64 (JSON) ──────────────────────────────────
|
// ── Чанковая загрузка 30KB ─────────────────────────────────
|
||||||
|
|
||||||
function uploadFileJSON(file, cid, onProgress) {
|
function uploadFileChunked(file, cid, onProgress) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
var reader = new FileReader();
|
var reader = new FileReader();
|
||||||
reader.onload = function() {
|
reader.onload = function() {
|
||||||
var b64 = reader.result.split(',')[1];
|
var b64 = reader.result.split(',')[1];
|
||||||
|
var CHUNK = 30 * 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);
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.open('POST', '/upload');
|
xhr.open('POST', '/chunk_json');
|
||||||
xhr.setRequestHeader('Content-Type', 'application/json');
|
xhr.setRequestHeader('Content-Type', 'application/json');
|
||||||
xhr.timeout = 30000;
|
xhr.timeout = 15000;
|
||||||
xhr.upload.onprogress = function(e) {
|
|
||||||
if (e.lengthComputable && onProgress) onProgress(Math.round(e.loaded / e.total * 100));
|
|
||||||
};
|
|
||||||
xhr.onload = function() {
|
xhr.onload = function() {
|
||||||
if (xhr.status === 200) {
|
if (xhr.status === 200) {
|
||||||
try { resolve(JSON.parse(xhr.responseText)); }
|
try {
|
||||||
catch(e) { reject(new Error('Bad JSON')); }
|
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));
|
||||||
|
setTimeout(function() { sendChunk(i + 1); }, 100);
|
||||||
|
} catch(e) { reject(new Error('Bad JSON')); }
|
||||||
} else { reject(new Error('HTTP ' + xhr.status)); }
|
} else { reject(new Error('HTTP ' + xhr.status)); }
|
||||||
};
|
};
|
||||||
xhr.onerror = function() { reject(new Error('Сеть')); };
|
xhr.onerror = function() { reject(new Error('Сеть')); };
|
||||||
xhr.ontimeout = function() { reject(new Error('Таймаут')); };
|
xhr.ontimeout = function() { reject(new Error('Таймаут')); };
|
||||||
xhr.send(JSON.stringify({filename: file.name, data: b64, cid: cid || null}));
|
xhr.send(JSON.stringify({
|
||||||
|
upload_id: uploadId,
|
||||||
|
filename: file.name,
|
||||||
|
chunk_index: i,
|
||||||
|
total_chunks: totalChunks,
|
||||||
|
data: piece,
|
||||||
|
cid: cid || null
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
sendChunk(0);
|
||||||
};
|
};
|
||||||
reader.onerror = function() { reject(new Error('Read error')); };
|
reader.onerror = function() { reject(new Error('Read error')); };
|
||||||
reader.readAsDataURL(file);
|
reader.readAsDataURL(file);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── Старая загрузка (одним POST) ─────────────────────────────
|
||||||
|
|
||||||
// ── Выбор файла → сразу загрузка ────────────────────────────
|
// ── Выбор файла → сразу загрузка ────────────────────────────
|
||||||
|
|
||||||
fileInput.addEventListener('change', async function() {
|
fileInput.addEventListener('change', async function() {
|
||||||
@@ -205,7 +227,7 @@
|
|||||||
var startTime = Date.now();
|
var startTime = Date.now();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var resp = await uploadFileJSON(f, contractId, function(pct) {
|
var resp = await uploadFileChunked(f, contractId, function(pct) {
|
||||||
fileQueue[rowIdx].status = '<span style="color:var(--muted);">↑ ' + pct + '%</span>';
|
fileQueue[rowIdx].status = '<span style="color:var(--muted);">↑ ' + pct + '%</span>';
|
||||||
renderTable();
|
renderTable();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user