dl_uniapp/utils/upload.js
2025-04-25 13:01:19 +08:00

41 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import cos from '@/utils/cos' // 您已有的COS实例
import {
getToken
} from '@/utils/auth'
import {
toast
} from '@/utils/common'
const upload = async (config) => {
try {
console.log('config', config);
// 生成文件名(避免中文乱码)
const fileName = '/images/' + config.filePath.split('/').pop(); // 提取最后一段路径
const data = await cos.uploadFile({
Bucket: 'notice-1348525010', // 填写自己的 bucket必须字段
Region: 'ap-beijing', // 存储桶所在地域,必须字段
Key: fileName,
FilePath: config.filePath,
/* v1.4.3之前的版本必须v1.4.3及以后的版本非必须 */
SliceSize: 1024 * 1024 * 5, // 触发分块上传的阈值超过5MB 使用分块上传小于5MB使用简单上传。可自行设置非必须
onProgress: function(progressData) {
console.log('上传进度:', progressData);
},
// onTaskReady: function(id) { // 非必须
// taskId = id;
// },
});
console.log('上传成功', data);
// 上传成功
const fileUrl = `https://${data.Location}`;
return {
url: fileUrl,
fileName: fileUrl
}
} catch (e) {
console.error('上传失败', e);
}
}
// 明确导出类型
export default upload