本项目包含两个核心前端服务:
// 在Vue/React组件或普通JS文件中
import { fileService } from './services/file-service.js';
import uploadService from './services/presigned-upload-service.js';
确保环境变量已配置:
VITE_API_URL=http://your-api-server.com
| 方法 | 参数 | 返回 | 示例 |
|---|---|---|---|
getFiles(params) |
{ page, limit, parentId, sort, order } | { files[], pagination, total } | await fileService.getFiles({ page: 1, limit: 20 }) |
uploadFile(file, options) |
File对象,{ onProgress, prefix } | { success, fileKey, url } | await uploadService.uploadFile(file, { onProgress: (p) => console.log(p) }) |
batchDownload(fileIds) |
[fileId1, fileId2, ...] | { success, fileCount, zipSize? } | await fileService.batchDownload(['id1', 'id2']) |
previewFile(fileId) |
文件ID字符串 | { success, url, fileType } | await fileService.previewFile('file-123') |
| 方法 | 说明 | 使用场景 |
|---|---|---|
getUploadUrl() | 获取临时上传URL | 自定义上传逻辑 |
uploadFileWithXHR() | 直接XHR上传 | 需要精细控制的上传 |
uploadFiles() | 批量上传 | 多文件上传界面 |
checkService() | 检查服务状态 | 上传前健康检查 |
// 1. 用户选择文件
const fileInput = document.getElementById('file-input');
const file = fileInput.files[0];
// 2. 使用PresignedUploadService上传到对象存储
const uploadResult = await uploadService.uploadFile(file, {
onProgress: (progress) => {
console.log(`上传进度: ${progress.percentage}%`);
}
});
// 3. 上传成功后,文件已保存在对象存储中
// (后端会自动记录,或可手动调用其他API)
// 加载文件列表
const { files } = await fileService.getFiles({ parentId: null });
// 渲染文件列表
files.forEach(file => {
// 根据文件类型显示不同图标
const canPreview = fileService.isPreviewSupported(file);
// 显示下载、预览、删除等按钮
});
// 处理文件操作
async function handleFileAction(fileId, action) {
switch(action) {
case 'download':
await fileService.downloadFile(fileId);
break;
case 'preview':
const preview = await fileService.previewFile(fileId);
openPreviewModal(preview.url);
break;
case 'delete':
await fileService.deleteFile(fileId);
break;
}
}
在开发环境下,可通过控制台直接调用服务:
// 检查服务状态
await uploadService.checkService();
// 测试上传
await window.testUpload('测试内容');
// 查看当前下载状态
fileService.downloadManager.activeDownloads;