window.onload = function(){
var xhr = new XMLHttpRequest();
xhr.open('GET', "http://localhost:8080/myblog/helloworld.pdf");
xhr.responseType = 'arraybuffer';
xhr.onreadystatechange = function getPdfOnreadystatechange(e) {
console.log("xhr.readyState:"+xhr.readyState+" xhr.status:"+xhr.status);
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
xhr.responseArrayBuffer || xhr.response);
console.log(data);
} else {
console.log("error");
}
}
};
xhr.send(null);
}
Ajax访问文件获取字节流方法
最新推荐文章于 2022-11-14 19:57:47 发布
本文介绍了一种使用JavaScript中的XMLHttpRequest对象从服务器获取并处理PDF文件的方法。具体实现包括设置HTTP请求方式、定义响应类型为ArrayBuffer以及根据不同状态码进行相应处理。
1049

被折叠的 条评论
为什么被折叠?



