Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用身份验证获取-文件下载_Javascript_Node.js_Fetch - Fatal编程技术网

Javascript 使用身份验证获取-文件下载

Javascript 使用身份验证获取-文件下载,javascript,node.js,fetch,Javascript,Node.js,Fetch,我正在尝试下载一个文档,其中包含一个需要身份验证的获取请求。代码本身工作正常,获取成功。但是,我不知道如何处理响应,因为它不是JSON或文本格式?它不会提示下载 有什么想法吗 export async function getDocument(authentication, file_link) { const url = "https://dtapiuat.datatree.com:443/api/Report/DownloadDocument?fileLink=" + file_l

我正在尝试下载一个文档,其中包含一个需要身份验证的获取请求。代码本身工作正常,获取成功。但是,我不知道如何处理响应,因为它不是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";
        }
    });
}

您需要创建一个带有Blob的URL并打开它

类似于下面的内容

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

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

您认为得到的输出是什么?您想要的输出是什么?您的控制台中是否出现CORS错误?正如@Tvde1所问的,我们需要知道响应是什么。你能发布控制台日志(httpResponse)的结果吗