将字节数组写入Javascript文件

将字节数组写入Javascript文件,javascript,arrays,rest,xmlhttprequest,downloadfile,Javascript,Arrays,Rest,Xmlhttprequest,Downloadfile,各位专家您好,我有一个java rest Web服务,它以字节数组的形式返回文档,我需要编写javascript代码来获取Web服务的响应并将其写入一个文件,以便将该文件下载为pdf。请查看Web服务响应的屏幕截图,并查看我的示例代码。此代码下载一个损坏的pdf文件 var data = new FormData(); data.append('PARAM1', 'Value1'); data.append('PARAM2', 'Value2');

各位专家您好,我有一个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 = "tst.pdf";
            // Append anchor to body.
            document.body.appendChild(a)
            a.click();
            // Remove anchor from body
            document.body.removeChild(a)

        };
        xhr.send(data);

`

由于您正在请求一个二进制文件,您也需要告诉XHR关于taht的信息,否则它将使用默认的“文本”(UTF-8)编码,将pdf解释为文本,并会弄乱编码。只需将
responseType
属性指定为pdf的MIME类型

        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 = "tst.pdf";
            // Append anchor to body.
            document.body.appendChild(a)
            a.click();
            // Remove anchor from body
            document.body.removeChild(a)

        };
        xhr.send(data);
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob'; // tell XHR that the response will be a pdf file

// OR xhr.responseType = 'application/pdf'; if above doesn't work
您将使用
response
属性而不是
responseText
访问它。 因此,您将使用
arr.push(xhr.response)将返回一个Blob

        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 = "tst.pdf";
            // Append anchor to body.
            document.body.appendChild(a)
            a.click();
            // Remove anchor from body
            document.body.removeChild(a)

        };
        xhr.send(data);
如果这不起作用,通知我将更新另一个解决方案

        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 = "tst.pdf";
            // Append anchor to body.
            document.body.appendChild(a)
            a.click();
            // Remove anchor from body
            document.body.removeChild(a)

        };
        xhr.send(data);
更新:

        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 = "tst.pdf";
            // Append anchor to body.
            document.body.appendChild(a)
            a.click();
            // Remove anchor from body
            document.body.removeChild(a)

        };
        xhr.send(data);
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob'; // tell XHR that the response will be a pdf file
xhr.onload = function() {
    var blob = this.response;
    var a = window.document.createElement('a');
    a.href = window.URL.createObjectURL(blob);
    a.download = "tst.pdf";
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
};

您好Viney谢谢您的帮助我尝试了您的想法,但我收到了以下错误“提供的值'application/pdf'不是有效的XMLHttpRequestResponseType类型枚举值”正在获取一个pdf或多个?仅获取一个文档谢谢它正在使用您的示例代码工作,非常感谢工具..这没什么。