feat: add drawer and add upload feat
This commit is contained in:
@@ -8,9 +8,11 @@ type ConvertOpts = {
|
||||
appKey?: string;
|
||||
version?: string;
|
||||
username?: string;
|
||||
directory?: string;
|
||||
};
|
||||
|
||||
export const uploadFileChunked = async (file: File, opts: ConvertOpts) => {
|
||||
const { directory } = opts;
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (!token) {
|
||||
@@ -22,7 +24,8 @@ export const uploadFileChunked = async (file: File, opts: ConvertOpts) => {
|
||||
const filename = file.name;
|
||||
const load = toast.loading(`${filename} 上传中...`);
|
||||
NProgress.start();
|
||||
const eventSource = new EventSource('http://49.232.155.236:11015/api/s1/events?taskId=' + taskId);
|
||||
// const eventSource = new EventSource('http://49.232.155.236:11015/api/s1/events?taskId=' + taskId);
|
||||
const eventSource = new EventSource('/api/s1/events?taskId=' + taskId);
|
||||
// 监听服务器推送的进度更新
|
||||
eventSource.onmessage = function (event) {
|
||||
console.log('Progress update:', event.data);
|
||||
@@ -60,7 +63,9 @@ export const uploadFileChunked = async (file: File, opts: ConvertOpts) => {
|
||||
formData.append('file', chunk, file.name);
|
||||
formData.append('chunkIndex', currentChunk.toString());
|
||||
formData.append('totalChunks', totalChunks.toString());
|
||||
|
||||
if (directory) {
|
||||
formData.append('directory', directory);
|
||||
}
|
||||
try {
|
||||
const res = await fetch('/api/s1/resources/upload/chunk?taskId=' + taskId, {
|
||||
method: 'POST',
|
||||
@@ -70,19 +75,17 @@ export const uploadFileChunked = async (file: File, opts: ConvertOpts) => {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
}).then((response) => response.json());
|
||||
|
||||
console.log(`Chunk ${currentChunk + 1}/${totalChunks} uploaded`, res);
|
||||
fetch('/api/s1/events/close?taskId=' + taskId);
|
||||
eventSource.close();
|
||||
NProgress.done();
|
||||
toast.dismiss(load);
|
||||
resolve(res);
|
||||
// console.log(`Chunk ${currentChunk + 1}/${totalChunks} uploaded`, res);
|
||||
} catch (error) {
|
||||
console.log('Error uploading chunk', error);
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fetch('/api/s1/events/close?taskId=' + taskId);
|
||||
eventSource.close();
|
||||
NProgress.done();
|
||||
toast.dismiss(load);
|
||||
resolve({ message: 'All chunks uploaded successfully' });
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user