核心方法:
upload(e) {
this.policy = Base64.encode(JSON.stringify(this.policyText))
this.audioName = e.target.files[0].name
const type = this.getType(e.target.files[0].name)
const key = Math.floor((new Date().getTime()) * Math.random() * 10)
const policy = this.policy
const OSSAccessKeyId = 'OSSAccessKeyId'
const signature = 'signature'
const url = getHost() // 阿里云地址
const param = new FormData()
param.append('name', `${this.audioName}`)
param.append('key', `${key + type}`)
param.append('policy', `${policy}`)
param.append('OSSAccessKeyId', `${OSSAccessKeyId}`)
param.append('success_action_status', 200)
param.append('signature', `${signature}`)
param.append('file', e.target.files[0], this.audioName)
this.loading = true
axios.post(url, param)
.then(response => {
this.resultURL = url + '/' + key + type // 返回访问的地址
})
.catch((err) => {
console.log(err)
})
},
signature签名是最重要的,如果没有签名,上传请求不会被通过,将导致无法上传,在vue环境下在下也没有搞懂如何获得签名,而是用了一个不靠谱的方法来解决(有大佬可以告诉怎么通过AccessKeySecret和policy计算签名吗)
成功上传到阿里云之后并不会返回访问地址,需要自己根据规则模拟。
policy必须是一个base64的字符串