Update storage.js via upload script - 2026-11-04 21:28:05
This commit is contained in:
parent
942e3ded49
commit
cf08d04079
54
storage.js
54
storage.js
|
|
@ -163,32 +163,40 @@ export async function saveTemplate(id, t, index) {
|
|||
}
|
||||
|
||||
// 上传一个与客户相关的沟通附件(文件本身存到磁盘,元数据由调用方存进客户记录)
|
||||
export async function uploadFile(customerId, file) {
|
||||
// onProgress(loaded, total):上传进度(字节),界面用它显示百分比。
|
||||
// 用 XMLHttpRequest 而不是 fetch:fetch 拿不到上传进度。大文件网速慢时能看到在动,不会以为卡住了。
|
||||
// 连续 STALL_MS 没有任何进度就中止并报错(网络断了、服务器没响应),不会一直停在"上传中"。
|
||||
const STALL_MS = 120 * 1000;
|
||||
export async function uploadFile(customerId, file, onProgress) {
|
||||
uploadsInFlight++;
|
||||
try { return await uploadFileNow(customerId, file); } finally { uploadsInFlight--; }
|
||||
try { return await uploadFileNow(customerId, file, onProgress); } finally { uploadsInFlight--; }
|
||||
}
|
||||
async function uploadFileNow(customerId, file) {
|
||||
const fd = new FormData();
|
||||
fd.append('file', file);
|
||||
const res = await fetch(`/api/uploads/${encodeURIComponent(customerId)}`, {
|
||||
method: 'POST',
|
||||
body: fd,
|
||||
function uploadFileNow(customerId, file, onProgress) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
let last = Date.now(), stalled = false;
|
||||
const timer = setInterval(() => {
|
||||
if (Date.now() - last > STALL_MS) { stalled = true; xhr.abort(); }
|
||||
}, 5000);
|
||||
const done = (fn, v) => { clearInterval(timer); fn(v); };
|
||||
xhr.open('POST', `/api/uploads/${encodeURIComponent(customerId)}`);
|
||||
xhr.upload.onprogress = (e) => { last = Date.now(); if (onProgress && e.lengthComputable) onProgress(e.loaded, e.total); };
|
||||
xhr.onload = () => {
|
||||
let data = null;
|
||||
try { data = JSON.parse(xhr.responseText); } catch (e) { /* 不是 JSON */ }
|
||||
if (xhr.status >= 200 && xhr.status < 300 && data) return done(resolve, data);
|
||||
let msg = (data && data.error) || `上传失败(HTTP ${xhr.status})`;
|
||||
// 响应不是 JSON——请求没到 Node 这一层,最常见的是 Nginx 的 client_max_body_size 比文件小,返回了 413
|
||||
if (xhr.status === 413) msg = `文件太大(${(file.size / 1048576).toFixed(1)} MB),被 Nginx 拦截了:Nginx 的 client_max_body_size 比这个文件小。需要在 Nginx 站点配置里调大(比如 client_max_body_size 200m;),然后执行 nginx -t 和 systemctl reload nginx`;
|
||||
else if (xhr.status === 504 || xhr.status === 502) msg = `服务器没有及时响应(HTTP ${xhr.status}),可能是 Nginx 的超时时间太短,文件可能没有保存,请重试`;
|
||||
done(reject, new Error(msg));
|
||||
};
|
||||
xhr.onerror = () => done(reject, new Error('网络中断,上传没有完成,请检查网络后重试'));
|
||||
xhr.onabort = () => done(reject, new Error(stalled ? `上传卡住了:${STALL_MS / 1000} 秒没有任何进度,可能是网络中断或服务器没有响应,请重试` : '上传已取消'));
|
||||
const fd = new FormData();
|
||||
fd.append('file', file);
|
||||
xhr.send(fd);
|
||||
});
|
||||
if (!res.ok) {
|
||||
let msg = `上传失败(HTTP ${res.status})`;
|
||||
try {
|
||||
const data = await res.json();
|
||||
if (data && data.error) msg = data.error;
|
||||
} catch (e) {
|
||||
// 响应不是JSON——说明请求可能根本没到Node这一层就被拦下了,最常见的原因是
|
||||
// Nginx反向代理的client_max_body_size(默认1MB)比文件小,直接返回了413的HTML错误页
|
||||
if (res.status === 413) {
|
||||
msg = '文件太大,被Nginx反向代理拦截了(Nginx的client_max_body_size配置比这个文件小,不一定是默认的1MB,也可能是之前设过的某个具体值)。需要去Nginx配置里把这个值调大,改完执行 nginx -t 和 systemctl reload nginx';
|
||||
}
|
||||
}
|
||||
throw new Error(msg);
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export async function deleteFile(customerId, fileName) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue