fix:修复文件上传报错

This commit is contained in:
hangyu.tao 2026-01-30 19:26:16 +08:00
parent 01927310cf
commit 66e5532139
3 changed files with 47 additions and 9 deletions

View File

@ -695,7 +695,8 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="createDescription">描述</label> <label for="createDescription">描述</label>
<textarea id="createDescription" name="description" rows="3" placeholder="请输入问题描述..."></textarea> <textarea id="createDescription" name="description" rows="3"
placeholder="请输入问题描述..."></textarea>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="createSolution">解决方案</label> <label for="createSolution">解决方案</label>
@ -1173,7 +1174,7 @@
</div> </div>
<!-- Scripts --> <!-- Scripts -->
<script src="/static/js/main.js?v=2.6"></script> <script src="/static/js/main.js?v=20260130-2222"></script>
<script src="/static/js/trial-periods.js?v=1.3"></script> <script src="/static/js/trial-periods.js?v=1.3"></script>
<script src="/static/js/trial-periods-page.js?v=1.6"></script> <script src="/static/js/trial-periods-page.js?v=1.6"></script>
</body> </body>

View File

@ -61,6 +61,7 @@ function canDelete() {
} }
document.addEventListener("DOMContentLoaded", function () { document.addEventListener("DOMContentLoaded", function () {
console.log("CRM System Main JS v3.5 Loaded - " + new Date().toLocaleString());
// 登录守卫 // 登录守卫
const token = localStorage.getItem("crmToken"); const token = localStorage.getItem("crmToken");
if (!token && !window.location.pathname.endsWith("login.html")) { if (!token && !window.location.pathname.endsWith("login.html")) {
@ -3248,16 +3249,52 @@ document.addEventListener("DOMContentLoaded", function () {
continue; continue;
} }
// Helper to convert file to base64 with retry
const fileToBase64 = async (f, attempt = 1) => {
try {
return await new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = () => reject(reader.error || new Error("FileReader error"));
reader.onabort = () => reject(new Error("File read aborted"));
reader.readAsDataURL(f);
});
} catch (err) {
if (attempt < 3 && err.name === "NotReadableError") {
console.warn(`Retrying ${f.name} read due to NotReadableError (attempt ${attempt})...`);
await new Promise(r => setTimeout(r, 200 * attempt));
return fileToBase64(f, attempt + 1);
}
throw err;
}
};
try { try {
const base64 = await new Promise((resolve, reject) => { console.log(`[v3.5] Processing ${file.name} (${file.size} bytes)...`);
const reader = new FileReader(); const base64 = await fileToBase64(file);
reader.onload = () => resolve(reader.result);
reader.onerror = (error) => reject(error);
reader.readAsDataURL(file);
});
newBase64Images.push(base64); newBase64Images.push(base64);
console.log(`Successfully converted ${file.name}`);
} catch (error) { } catch (error) {
console.error(`Error converting file ${file.name} to base64:`, error); console.error(`Primary method failed for ${file.name}:`, error);
// Fallback: arrayBuffer
try {
console.log(`Trying arrayBuffer fallback for ${file.name}...`);
const buffer = await file.arrayBuffer();
const bytes = new Uint8Array(buffer);
let binary = "";
const chunkSize = 8192;
for (let i = 0; i < bytes.length; i += chunkSize) {
const chunk = bytes.slice(i, i + chunkSize);
binary += String.fromCharCode.apply(null, chunk);
}
const b64 = `data:${file.type};base64,${btoa(binary)}`;
newBase64Images.push(b64);
console.log(`Fallback successful for ${file.name}`);
} catch (fallbackError) {
console.error(`Both methods failed for ${file.name}:`, fallbackError);
alert(`无法读取文件 "${file.name}"。\n这种情况通常是浏览器或系统文件锁定导致的。\n\n建议尝试:\n1. 刷新页面后再试\n2. 使用浏览器的“无痕模式”\n3. 确认文件没有被其他软件打开`);
}
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 172 KiB