服务端向客户端游览器发送文件时,如果是浏览器支持的文件类型,一般会默认使用浏览器打开,比如txt、jpg等,会直接在浏览器中显示,如果需要提示用户保存,就要利用Content-Disposition进行一下处理,关键在于一定要加上attachment:
方法一:
通过阿里云后台配置http头 Content-Disposition 为 attachment 但是这种方法 会让拿到链接想播放的人也会默认下载
方法二:
let OSS = require('ali-oss');
let client = new OSS({
region: '<Your region>',
accessKeyId: '<Your AccessKeyId>',
accessKeySecret: '<Your AccessKeySecret>',
bucket: 'Your bucket name'
});
let url1 = client.signatureUrl('object-name', {
response: {
'content-disposition': 'attachment; filename="' + name + '.' + type + '"'
}
})
let url2 = client.signatureUrl('object-name')
获取到的url1就是可下载地址啦,可以写在a标签里面,加上download属性就可以下载了
获取到的url2就是默认的播放地址(前提是后台没有配置Content-Disposition 为 attachment)
阿里云下载文件方法参考官方手册 https://help.aliyun.com/document_detail/32073.html?spm=a2c4g.11186623.6.931.258943c4lNr2C1