我正在尝试在按钮单击事件上从 Firebase 存储中下载文件,但它给了我“Access-Control-Allow-Origin”错误。
https://firebase.google.com/docs/storage/web/download-files
根据上面的链接,我正在尝试配置 CORS。我安装了 gsutil 命令行工具。但我无法找到需要复制此代码的 cors.json 文件
[
{
"origin": ["*"],
"method": ["GET"],
"maxAgeSeconds": 3600
}
]
然后我使用命令gsutil cors get gs://<your-cloud-storage-bucket>
返回gsutil cors get gs://<your-cloud-storage-bucket>/ has no CORS configuration
.
我是否需要先为我的存储桶创建 CORS 配置文件?
下面是我的按钮单击方法,以防错误出现在代码中。
downloadAttachment(fileName) {
var uid = AuthService.uid;
let storageRef = firebase.storage().ref().child('assignments/' + uid + '/' + fileName);
storageRef.getDownloadURL().then(url => {
console.log(url);
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function (event) {
var blob = xhr.response;
};
xhr.open('GET', url);
xhr.send();
});
}
Error :
请求的资源上不存在“Access-Control-Allow-Origin”标头。起源 'http://本地主机:4200' 因此不允许访问。
Thanks.
是的,创建一个文件,命名为您想要的任何名称 - 调用它cors.json
如果您愿意,请将您首选的 CORS 配置设置放入其中,然后运行gsutil
像这样:
gsutil cors set cors.json gs://<your-cloud-storage-bucket>/
这会将这些 CORS 配置设置上传到您的存储桶。之后您可以随时使用以下命令检索当前设置gsutil cors get gs://<your-cloud-storage-bucket>
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)