我在用Obs直传文件时一直报400,前端和后端分析了一段时间,一直在报400
解决方案 formData.append('file', file.file) file一定要放到最后,不然就会报400
const res = await fetch(stsCredential.value + `?fileName=${fileName}`, { method: 'GET', headers: getHeaders() })
const dataRes = await res.json()
if (dataRes.code !== 100000) {
ElMessage.error('获取文件上传签名失败')
throw new Error('获取文件上传签名失败')
}
const data = dataRes.data
const filePath = data.objectKey
// 构造 FormData
const formData = new FormData()
formData.append('key', data.objectKey);
formData.append('x-obs-acl', 'public-read');
formData.append('policy', data.policy);
formData.append('signature', data.signature);
formData.append('AccessKeyId', data.accessKeyId)
formData.append('file', file.file) // file一定要放到最后,不然就会报400
let loading = ElLoading.service({ fullscreen: true })
const uploadRes = await fetch(data.uploadUrl, {
method: 'POST',
body: formData
})