我有 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)