我正在尝试使用Internet Explorer(在用C#编写并使用WatIn库编写的剪贴应用程序中)下载pdf文件内容。我试图从中下载文件的网站使用带有安全cookie的身份验证,因此我无法获取cookie并使用HTTPClient
下载文件。这就是为什么我需要从Javascript获取文件内容的原因。
我设法实现了这一点,但它仅适用于IE10 +。
这是我使用的代码:
(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);
此代码的问题在于它不适用于IE9和更早版本。我至少需要涵盖IE8和IE9
参考方案
据我了解,您在从AJAX响应中获取二进制内容时遇到问题,该项目旨在处理以下问题:https://github.com/Stuk/jszip-utils
如您在getBinaryContent函数(jszip-utils.js中的_getBinaryFromXHR调用)的xhr处理部分中所见,在旧IE上,您需要通过VBScript而不是标准xhr响应从xhr获取数据(请参阅jszip- utils-ie.js,其中_getBinaryFromXHR被覆盖)。
SO answer中的另一种结构更老套的方法的例子(虽然还没有测试过)。
如果我得到url(''),我该如何使用另一个URL - javascript我是新手,正在写这篇文章,但是如果源上没有图像,那么我只有空白。有人可以告诉我,如果我正在获取背景图像,如何获取/images/no-image.jpg:url();这是我的代码:<div class="uk-clearfix uk-position-relative"> <div class="recipeb…
对ID为'abc%'的dom执行操作 - javascript我想对ID为'abc%'的DOM进行一些操作<a id='abc1'></a> <a id='abc2'></a> <a id='abc3'></a> <a id='abc4'></a>…
Jsonp没有出现``访问控制允许来源''错误 - javascript在我的PHP中,我喜欢这样来回显jsonp类型的“ json数据”echo $_GET['callback'] . '('.json_encode($arr).')'; 在我的js(angularjs)中,$http.get('http://example.com/app/?callbac…
Javascript IF语句 - javascript嗨,我有这段代码可以正常工作,并将两个日历显示为一个日历。我还有一个php变量$login_session,其中包含登录电子邮件地址的用户。关于如何显示[email protected]日历的任何想法(伪代码)IF $login_session == "[email protected]…
javascript popupwindow之后的行如何工作? - javascript好的,我有一个来自后面代码的方法,可以创建一个popupwindow。然后有一行代码要在那之后执行,我想知道那行代码何时执行,是在使用popupwindow之后执行还是在创建popupwindow之后执行?例如:void exPopupWindowMethod() { string scr = "window.open('exampleP…