- 优化界面全屏、视频上传进度条、视频自动裁切铺满屏幕
This commit is contained in:
+40
-1
@@ -59,7 +59,46 @@ export async function uploadFiles(files) {
|
||||
for (const file of files) {
|
||||
const fd = new FormData();
|
||||
fd.append('file', file);
|
||||
await fetch(`${BASE}/upload`, { method: 'POST', body: fd });
|
||||
const res = await fetch(`${BASE}/upload`, { method: 'POST', body: fd });
|
||||
if (!res.ok) throw new Error(`上传失败 (${res.status})`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function uploadFilesWithProgress(files, onProgress) {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
await new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', `${BASE}/upload`);
|
||||
|
||||
xhr.upload.onprogress = (e) => {
|
||||
if (e.lengthComputable) {
|
||||
onProgress({
|
||||
percent: Math.round((e.loaded / e.total) * 100),
|
||||
loaded: e.loaded,
|
||||
total: e.total,
|
||||
fileName: file.name,
|
||||
current: i + 1,
|
||||
total: files.length,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onload = () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
resolve();
|
||||
} else {
|
||||
reject(new Error(`上传失败 (${xhr.status})`));
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onerror = () => reject(new Error('网络错误,请检查连接'));
|
||||
xhr.ontimeout = () => reject(new Error('上传超时'));
|
||||
|
||||
const fd = new FormData();
|
||||
fd.append('file', file);
|
||||
xhr.send(fd);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user