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-01-12 17:01:08 发布
本文演示了如何使用JavaScript的XMLHttpRequest对象从本地服务器获取并解析PDF文件。通过设置响应类型为arraybuffer,可以接收PDF文件的原始二进制数据,便于进一步处理或展示。
1047

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



