|
|
@ -119,13 +119,38 @@ export class DirectUpload { |
|
|
file: File, |
|
|
file: File, |
|
|
credentials: UploadCredentials, |
|
|
credentials: UploadCredentials, |
|
|
onProgress?: (percent: number) => void |
|
|
onProgress?: (percent: number) => void |
|
|
|
|
|
): Promise<UploadResult> { |
|
|
|
|
|
return new Promise(async (resolve, reject) => { |
|
|
|
|
|
const filePath = credentials.file_path.replace('${filename}', file.name) |
|
|
|
|
|
|
|
|
|
|
|
// 尝试直传,如果CORS失败则使用代理
|
|
|
|
|
|
try { |
|
|
|
|
|
const directResult = await this.directUploadToTencent(file, credentials, filePath, onProgress) |
|
|
|
|
|
resolve(directResult) |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.warn('Direct upload failed, trying proxy:', error) |
|
|
|
|
|
try { |
|
|
|
|
|
const proxyResult = await this.proxyUploadToTencent(file, credentials, filePath, onProgress) |
|
|
|
|
|
resolve(proxyResult) |
|
|
|
|
|
} catch (proxyError) { |
|
|
|
|
|
reject(proxyError) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 腾讯云COS直接上传 |
|
|
|
|
|
*/ |
|
|
|
|
|
private async directUploadToTencent( |
|
|
|
|
|
file: File, |
|
|
|
|
|
credentials: UploadCredentials, |
|
|
|
|
|
filePath: string, |
|
|
|
|
|
onProgress?: (percent: number) => void |
|
|
): Promise<UploadResult> { |
|
|
): Promise<UploadResult> { |
|
|
return new Promise((resolve, reject) => { |
|
|
return new Promise((resolve, reject) => { |
|
|
const formData = new FormData() |
|
|
const formData = new FormData() |
|
|
|
|
|
|
|
|
// 生成文件路径(替换模板中的${filename})
|
|
|
|
|
|
const filePath = credentials.file_path.replace('${filename}', file.name) |
|
|
|
|
|
|
|
|
|
|
|
// 添加腾讯云COS必需的字段
|
|
|
// 添加腾讯云COS必需的字段
|
|
|
formData.append('key', filePath) |
|
|
formData.append('key', filePath) |
|
|
formData.append('policy', credentials.credentials.policy) |
|
|
formData.append('policy', credentials.credentials.policy) |
|
|
@ -171,6 +196,69 @@ export class DirectUpload { |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 腾讯云COS代理上传(开发环境临时方案) |
|
|
|
|
|
*/ |
|
|
|
|
|
private async proxyUploadToTencent( |
|
|
|
|
|
file: File, |
|
|
|
|
|
credentials: UploadCredentials, |
|
|
|
|
|
filePath: string, |
|
|
|
|
|
onProgress?: (percent: number) => void |
|
|
|
|
|
): Promise<UploadResult> { |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
const formData = new FormData() |
|
|
|
|
|
|
|
|
|
|
|
// 添加后端代理需要的参数(分别发送各个字段,避免JSON解析问题)
|
|
|
|
|
|
formData.append('upload_url', credentials.upload_url) |
|
|
|
|
|
formData.append('key', filePath) |
|
|
|
|
|
formData.append('policy', credentials.credentials.policy) |
|
|
|
|
|
formData.append('q-sign-algorithm', credentials.credentials['q-sign-algorithm']) |
|
|
|
|
|
formData.append('q-ak', credentials.credentials['q-ak']) |
|
|
|
|
|
formData.append('q-key-time', credentials.credentials['q-key-time']) |
|
|
|
|
|
formData.append('q-signature', credentials.credentials['q-signature']) |
|
|
|
|
|
formData.append('domain', credentials.domain) |
|
|
|
|
|
formData.append('file', file) |
|
|
|
|
|
|
|
|
|
|
|
const xhr = new XMLHttpRequest() |
|
|
|
|
|
|
|
|
|
|
|
// 上传进度
|
|
|
|
|
|
if (onProgress) { |
|
|
|
|
|
xhr.upload.addEventListener('progress', (event) => { |
|
|
|
|
|
if (event.lengthComputable) { |
|
|
|
|
|
const percent = Math.round((event.loaded / event.total) * 100) |
|
|
|
|
|
onProgress(percent) |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 上传完成
|
|
|
|
|
|
xhr.addEventListener('load', () => { |
|
|
|
|
|
try { |
|
|
|
|
|
const response = JSON.parse(xhr.responseText) |
|
|
|
|
|
if (response.code === 1) { |
|
|
|
|
|
resolve({ |
|
|
|
|
|
success: true, |
|
|
|
|
|
url: response.data.url |
|
|
|
|
|
}) |
|
|
|
|
|
} else { |
|
|
|
|
|
reject(new Error(response.msg || '上传失败')) |
|
|
|
|
|
} |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
reject(new Error('响应解析失败')) |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
// 上传错误
|
|
|
|
|
|
xhr.addEventListener('error', () => { |
|
|
|
|
|
reject(new Error('网络错误,上传失败')) |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
// 执行代理上传
|
|
|
|
|
|
xhr.open('POST', `${this.baseUrl}/adminapi/sys/direct/cos_proxy`, true) |
|
|
|
|
|
xhr.send(formData) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 阿里云OSS直传 |
|
|
* 阿里云OSS直传 |
|
|
*/ |
|
|
*/ |
|
|
|