需求下载oss图片到本地转为 base64 的时候,发生了跨域
oss 跨域规则设置:
解决方法1:打开控制台勾选停用缓存(disable cache)
解决方法2:修改阿里云上传的方法,手动添加’Cache-Control’ => ‘no-cache’
const getFileBlob = (url: string): Promise<Blob> => {
return new Promise((resolve) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.responseType = "blob";
xhr.setRequestHeader("Cache-Control", "no-cache"); //’Cache-Control’ => ‘no-cache’
xhr.onload = () => {
if (xhr.status === 200) {
resolve(xhr.response);
}
};
xhr.send();
});
解决方法3:下载地址加上随机参数,url = url + ‘?s=’ + Math.random().toString()