0

I'm trying to download pdf file content using internet explorer (in a scrapping application written with C# and using WatIn library). The website from which I'm trying to download the file uses authentication with secure cookies, so I can't get the cookies and download the file using a HTTPClient. That's why I need to get the file content from Javascript. I managed to achieve that, but it only works with IE10+. Here is the code I used:

(function() {
    try {
        var a = new Uint8Array(1);
        return;
    } catch (e) {}

    function subarray(start, end) {
        return this.slice(start, end);
    }

    function set_(array, offset) {
        if (arguments.length < 2) offset = 0;
        for (var i = 0, n = array.length; i < n; ++i, ++offset) this[offset] = array[i] & 0xFF;
    }

    function TypedArray(arg1) {
        var result;
        if (typeof arg1 === 'number') {
            result = new Array(arg1);
            for (var i = 0; i < arg1; ++i) result[i] = 0;
        } else result = arg1.slice(0);
        result.subarray = subarray;
        result.buffer = result;
        result.byteLength = result.length;
        result.set = set_;
        if (typeof arg1 === 'object' && arg1.buffer) result.buffer = arg1.buffer;
        return result;
    }
    window.Uint8Array = TypedArray;
    window.Uint32Array = TypedArray;
    window.Int32Array = TypedArray;
})();

function _arrayBufferToBase64(buffer) {
    var binary = '';
    var bytes = new Uint8Array(buffer);
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode(bytes[i]);
    }
    return window.btoa(binary);
}
var callFinished = false;
var callPDFResult = '';
var xhr = new XMLHttpRequest();
xhr.open('GET', 'fileUrl', true);
if ('responseType' in xhr) {
    xhr.responseType = 'arraybuffer';
 } else if ('overrideMimeType' in xhr) {
    xhr.overrideMimeType('text/plain; charset=x-user-defined');
} else {
    xhr.setRequestHeader('Accept-Charset', 'x-user-defined');
}
xhr.onreadystatechange = function() {
    if (xhr.readyState == XMLHttpRequest.DONE) {
        callFinished = true;
        callPDFResult = _arrayBufferToBase64(xhr.response);
    }
};
xhr.send(null);

The problem with this code is that it doesn't work with IE9 and earlier versions. I need to cover IE8 and IE9 at least

Kira
  • 1,153
  • 4
  • 28
  • 63

1 Answers1

1

As I understand it you have problems getting binary content from an AJAX response, this project is meant to deal with exactly that: https://github.com/Stuk/jszip-utils

As you can see in the xhr processing part in the getBinaryContent function (_getBinaryFromXHR call in jszip-utils.js), on the old IEs you need to get the data from the xhr by a VBScript instead of the standard xhr response (see jszip-utils-ie.js where the _getBinaryFromXHR is overriden).

Another example of the same approach structured more old-schooley way is in this SO answer (haven't tested this though).

Tomas Varga
  • 1,432
  • 15
  • 23