项目存储文件由服务器转为阿里云oss存储后,返回的是https的url链接,导致页面无法直接打开或重命名下载。两步解决问题
1、阿里云OSS跨域设置必须配置你的域名规则或者配置 * 全匹配
2、例如一个pdf文件需要在新窗口预览或下载;我们可以用XMLHttpRequest或者axios重发下转为文件流在打开:
const xhr = new XMLHttpRequest();
xhr.open("get", '你的URL');
xhr.responseType = "blob";
xhr.withCredentials = false;//这里必须设置为false
xhr.send();
xhr.onload = function () {
if (this.status === 200 || this.status === 304) {
const fileReader = new FileReader();
fileReader.readAsDataURL(this.response);
fileReader.onload = function () {
const a = document.createElement("a");
a.style.display = "none";
a.href = this.result;
a.download = '文件名';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
}
};