v1.0.35: iframe + form-encoded base64

This commit is contained in:
2026-06-18 21:26:18 +04:00
parent 0a3ccdb973
commit b2b148c1b7
2 changed files with 75 additions and 35 deletions
+35 -31
View File
@@ -64,7 +64,7 @@
<body> <body>
<div class="topbar"> <div class="topbar">
<img src="/nubes-logo.svg" alt="Nubes"> <img src="/nubes-logo.svg" alt="Nubes">
<span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.0.34 — Lucee</span></span> <span class="title">Сверка договоров <span style="font-weight:400;color:var(--muted);font-size:12px;">v1.0.35 — Lucee</span></span>
</div> </div>
<div class="content"> <div class="content">
@@ -221,7 +221,7 @@ fileInput.addEventListener('change', function() {
renderTable(); renderTable();
}); });
// ── Загрузка через iframe (отдельный контекст на каждый файл) // ── Загрузка через iframe (base64 в скрытом поле) ───────
uploadBtn.addEventListener('click', function() { uploadBtn.addEventListener('click', function() {
if (fileQueue.length === 0) return; if (fileQueue.length === 0) return;
uploadBtn.disabled = true; uploadBtn.disabled = true;
@@ -231,7 +231,7 @@ uploadBtn.addEventListener('click', function() {
var pending = fileQueue.filter(function(f) { return !f.doc_id; }); var pending = fileQueue.filter(function(f) { return !f.doc_id; });
var done = 0; var done = 0;
function uploadNext(i) { function readThenUpload(i) {
if (i >= pending.length) { if (i >= pending.length) {
uploadTimer.style.display = 'none'; uploadTimer.style.display = 'none';
uploadBtn.disabled = false; uploadBtn.disabled = false;
@@ -242,18 +242,34 @@ uploadBtn.addEventListener('click', function() {
return; return;
} }
var f = pending[i]; var f = pending[i];
f.status = 'читаю...';
renderTable();
uploadTimer.textContent = 'Чтение ' + (i+1) + '/' + pending.length;
var reader = new FileReader();
reader.onload = function(e) {
var b64 = e.target.result.split(',')[1];
f.status = '0%'; f.status = '0%';
renderTable(); renderTable();
uploadTimer.textContent = 'Загрузка ' + (i+1) + '/' + pending.length; uploadTimer.textContent = 'Загрузка ' + (i+1) + '/' + pending.length;
submitFile(i, f, b64);
};
reader.onerror = function() {
f.status = 'error: чтение';
renderTable();
readThenUpload(i + 1);
};
reader.readAsDataURL(f.file);
}
// Создаём уникальный iframe function submitFile(i, f, b64) {
var iframeId = 'up_iframe_' + Date.now() + '_' + i;
var iframe = document.createElement('iframe'); var iframe = document.createElement('iframe');
iframe.name = iframeId; iframe.name = 'upfrm_' + Date.now() + '_' + i;
iframe.style.display = 'none'; iframe.style.display = 'none';
iframe.onload = function() { iframe.onload = function() {
try { try {
var resp = iframe.contentDocument.body.textContent; var resp = iframe.contentDocument.body.textContent.trim();
if (!resp) { throw new Error('пустой ответ'); }
var r = JSON.parse(resp); var r = JSON.parse(resp);
if (r.OK) { if (r.OK) {
contractId = r.CONTRACT_ID; contractId = r.CONTRACT_ID;
@@ -272,38 +288,26 @@ uploadBtn.addEventListener('click', function() {
} }
renderTable(); renderTable();
document.body.removeChild(iframe); document.body.removeChild(iframe);
uploadNext(i + 1); readThenUpload(i + 1);
}; };
document.body.appendChild(iframe); document.body.appendChild(iframe);
// Форма внутри iframe // Форма с скрытыми полями, target = iframe
var form = iframe.contentDocument.createElement('form'); var form = document.createElement('form');
form.method = 'POST'; form.method = 'POST';
form.action = '/upload.cfm'; form.action = '/upload.cfm';
form.enctype = 'multipart/form-data'; form.target = iframe.name;
form.style.display = 'none';
var input = iframe.contentDocument.createElement('input'); form.innerHTML =
input.type = 'file'; '<input type="hidden" name="filename" value="' + f.name.replace(/"/g,'&quot;') + '">' +
input.name = 'files'; '<input type="hidden" name="data" value="' + b64.replace(/"/g,'&quot;') + '">' +
// Копируем файл в форму через DataTransfer (contractId ? '<input type="hidden" name="contract_id" value="' + contractId + '">' : '');
var dt = new DataTransfer(); document.body.appendChild(form);
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(); form.submit();
setTimeout(function() { document.body.removeChild(form); }, 100);
} }
uploadNext(0); readThenUpload(0);
}); });
// ── Инфо ────────────────────────────────────────────────── // ── Инфо ──────────────────────────────────────────────────
+38 -2
View File
@@ -12,8 +12,44 @@
<cftry> <cftry>
<cfset ct = cgi.http_content_type> <cfset ct = cgi.http_content_type>
<!--- JSON: {filename, data} или {files: [{filename, data}]} ---> <!--- Form-encoded: filename=&data=&contract_id= (iframe) --->
<cfif ct CONTAINS "application/json"> <cfif structKeyExists(form, "filename") AND structKeyExists(form, "data")>
<cfset clientFile = form.filename>
<cfset originalBytes = form.data>
<cfset contractId = structKeyExists(form, "contract_id") ? form.contract_id : "">
<cfset mime = "application/octet-stream">
<cfset ext = listLast(clientFile, ".")>
<cfif ext EQ "pdf"><cfset mime = "application/pdf">
<cfelseif ext EQ "docx"><cfset mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document">
<cfelseif ext EQ "doc"><cfset mime = "application/msword">
<cfelseif ext EQ "zip"><cfset mime = "application/zip">
</cfif>
<cftransaction>
<cfquery datasource="baza">
INSERT INTO documents (filename, mime_type, original_bytes, status) VALUES (<cfqueryparam value="#clientFile#" cfsqltype="cf_sql_varchar">,<cfqueryparam value="#mime#" cfsqltype="cf_sql_varchar">,<cfqueryparam value="#originalBytes#" cfsqltype="cf_sql_varchar">,'uploaded')
</cfquery>
<cfquery name="doc" datasource="baza">
SELECT id FROM documents WHERE filename=<cfqueryparam value="#clientFile#" cfsqltype="cf_sql_varchar"> AND status='uploaded' ORDER BY created_at DESC LIMIT 1
</cfquery>
<cfif len(contractId)>
<cfset suppType = "additional">
<cfelse>
<cfquery datasource="baza">
INSERT INTO contracts (number, client) VALUES (<cfqueryparam value="б/н #dateFormat(now(),'yyyymmdd')#-#timeFormat(now(),'HHmm')#" cfsqltype="cf_sql_varchar">,'')
</cfquery>
<cfquery name="cid" datasource="baza">
SELECT id FROM contracts ORDER BY created_at DESC LIMIT 1
</cfquery>
<cfset contractId = cid.id>
<cfset suppType = "initial">
</cfif>
<cfquery datasource="baza">
INSERT INTO supplements (contract_id, document_id, type) VALUES (<cfqueryparam value="#contractId#" cfsqltype="cf_sql_varchar">,<cfqueryparam value="#doc.id#" cfsqltype="cf_sql_varchar">,<cfqueryparam value="#suppType#" cfsqltype="cf_sql_varchar">)
</cfquery>
</cftransaction>
<cfset result = {ok: true, contract_id: contractId, doc_id: doc.id, filename: clientFile, size: len(originalBytes)}>
<!--- JSON: {filename, data} или {files: [...]} --->
<cfelseif ct CONTAINS "application/json">
<cfset body = toString(getHttpRequestData().content)> <cfset body = toString(getHttpRequestData().content)>
<cfset data = deserializeJSON(body)> <cfset data = deserializeJSON(body)>
<cfif structKeyExists(data, "files") AND isArray(data.files)> <cfif structKeyExists(data, "files") AND isArray(data.files)>