如何从 Firebase 存储下载 pdf

问题描述

当我尝试从 Firebase Storage 下载 pdf 时遇到此错误Access to XMLHttpRequest at "document.pdf" from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. 现在我已将存储桶的 cors.js 配置为没有任何限制。但是当我点击下载按钮时,什么也没有打开,什么也没有下载。

我的网页中有一个按钮,onClick 它会调用客户端的 downloadInvoice() 函数

function downloadInvoice(){
    var invoiceRef = firebase.storage().ref('pedidos/' + id +'.pdf');

    // Get the download URL
    invoiceRef.getDownloadURL().then(function(url) {
    // This can be downloaded directly:
    var xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = function(event) {
        var blob = xhr.response;
    };
    xhr.open('GET',url);
    xhr.send();
    }).catch(function(error) {

        // A full list of error codes is available at
        // https://firebase.google.com/docs/storage/web/handle-errors
        switch (error.code) {
            case 'storage/object-not-found':
            // File doesn't exist
            break;

            case 'storage/unauthorized':
            // User doesn't have permission to access the object
            break;

            case 'storage/canceled':
            // User canceled the upload
            break;

            case 'storage/unkNown':
            // UnkNown error occurred,inspect the server response
            break;
        }
    });
}

URL 是正确的,因为当我遇到错误时,我可以从浏览器的控制台打开文档。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...