// 创建 OSS 客户端对象
createOssClientTemp () {
return new Promise((resolve) => {
let client = new OSS({
accessKeyId: 'xxx',
accessKeySecret: 'xxx',
bucket: 'xxx'
})
resolve(client)
})
},
// OSS 文件下载
ossDownloadFile (objectKey) {
this.createOssClientTemp().then( client => {
let url = client.signatureUrl(objectKey, {
response: {
'content-disposition': 'attachment; filename="' + name + '.' + type + '"'
},
expires: 3600
})
console.log(url)
window.location.href = url
})
}
Ps:一般 OSS 下载都会遇到跨域文件下载问题,当然有时候如果电脑有安装下载检测浏览器时,可以避免,但是为了考虑周全,还是要想一个通用的办法。
注意事项
- content-disposition: attachment;:解决了跨域请求的问题,前提在 OSS 配置好跨域。
- filename="' + name + '.' + type + '":下载时,重命名,name(名字),type(后缀)。