小编use*_*595的帖子

将字节数组写入文件 JavaScript

我有 Java REST Web 服务,它以字节数组的形式返回文档,我需要编写 JavaScript 代码来获取 Web 服务的响应并将其写入文件,以便将该文件下载为 PDF 请查看 Web 服务响应的屏幕截图并查看我的示例代码 此代码下载损坏的 PDF 文件。

var data = new FormData();
data.append('PARAM1', 'Value1');
data.append('PARAM2', 'Value2');
var xhr = new XMLHttpRequest();
xhr.open('POST', 'SERVICEURL');
xhr.withCredentials = true;
xhr.setRequestHeader("Authorization", "Basic " + btoa("username:password"));
xhr.onload = function() {
    
    console.log('Response text = ' + xhr.responseText);
    console.log('Returned status = ' + xhr.status);
    
    
    var arr = [];
    arr.push(xhr.responseText);

    var byteArray = new Uint8Array(arr);
    var a = window.document.createElement('a');
    a.href = window.URL.createObjectURL(new Blob(byteArray, { type: 'application/octet-stream' }));
    a.download = …
Run Code Online (Sandbox Code Playgroud)

javascript arrays rest xmlhttprequest download

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

arrays ×1

download ×1

javascript ×1

rest ×1

xmlhttprequest ×1