通过身份验证获取-文件下载

时间:2018-08-28 15:19:47

标签: javascript node.js fetch

我正在尝试下载带有要求进行身份验证的提取请求的文档。代码本身可以正常工作,并且提取成功。但是,我不知道如何处理响应,因为它不是JSON或文本格式?它不会提示下载。

有什么想法吗?

export async function getDocument(authentication, file_link) {

    const url = "https://dtapiuat.datatree.com:443/api/Report/DownloadDocument?fileLink=" + file_link;
    console.log("Retrieve Customer URL: " + url);

    return fetch(url, {
        method: 'get',
        headers: {
            'Accept': 'application/json',
            'Content-Type': "application/json",
            'Authorization': authentication
        },

    })
    .then(async(httpResponse) => {
        if (httpResponse.ok) {
            console.log(httpResponse.ok);
            return httpResponse;
        } else {
            return "Fetch did not succeed";
        }
    });
}

1 个答案:

答案 0 :(得分:0)

您将需要使用Blob创建一个URL并将其打开。

类似下面的内容。

download(data){
  var blob = new Blob([data], { type: 'text/csv' });
  var url= window.URL.createObjectURL(blob);
  window.open(url);
}

注意-这不会显示文件下载进度。如果文件很大,则可能需要显示手动进度条。

相关问题