当前位置: 代码迷 >> 综合 >> web端直传文件到阿里云
  详细解决方案

web端直传文件到阿里云

热度:98   发布时间:2024-01-24 06:15:46.0

核心方法:

upload(e) {this.policy = Base64.encode(JSON.stringify(this.policyText))this.audioName = e.target.files[0].nameconst type = this.getType(e.target.files[0].name)const key = Math.floor((new Date().getTime()) * Math.random() * 10)const policy = this.policyconst 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 = trueaxios.post(url, param).then(response => {this.resultURL = url + '/' + key + type    // 返回访问的地址}).catch((err) => {console.log(err)})
},

signature签名是最重要的,如果没有签名,上传请求不会被通过,将导致无法上传,在vue环境下在下也没有搞懂如何获得签名,而是用了一个不靠谱的方法来解决(有大佬可以告诉怎么通过AccessKeySecret和policy计算签名吗)

成功上传到阿里云之后并不会返回访问地址,需要自己根据规则模拟。

policy必须是一个base64的字符串

  相关解决方案