Ant*_*ton 7 javascript jquery html5 binary-data
我正在对一个返回二进制数据的API进行ajax调用.我想知道是否可以获取二进制数据并在新窗口中为客户端显示它?这就是我现在正在做的事情.问题是,文档打开了,但它完全是空白的.
$.ajax({
type: "POST",
url: apiURL,
data: xmlRequest,
complete: function(xhr, status) {
var bb = new window.WebKitBlobBuilder();
// Append the binary data to the blob
bb.append(xhr.responseText);
var blobURL = window.webkitURL.createObjectURL(bb.getBlob('application/pdf'));
window.open(blobURL);
}
});
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
好的,我明白了.我必须将responseType指定为'array buffer':
function downloadPDF() {
var xhr = new XMLHttpRequest();
xhr.open('POST', API_URL, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
if (this.status == 200) {
var bb = new window.WebKitBlobBuilder();
bb.append(this.response); // Note: not xhr.responseText
var blob = bb.getBlob('application/pdf');
var blobURL = window.webkitURL.createObjectURL(blob);
window.open(blobURL);
}
};
xhr.send(createRequest());
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15319 次 |
| 最近记录: |