Ionic framework 在ionic 2中从XHR请求获取BLOB数据
任何人都可以告诉我如何在ionic 2中获取Blob XHR请求和XMLHttpRequest,我必须从api获取图像url 这是我的Ajax代码,我想在爱奥尼亚2Ionic framework 在ionic 2中从XHR请求获取BLOB数据,ionic-framework,ionic2,xmlhttprequest,blob,Ionic Framework,Ionic2,Xmlhttprequest,Blob,任何人都可以告诉我如何在ionic 2中获取Blob XHR请求和XMLHttpRequest,我必须从api获取图像url 这是我的Ajax代码,我想在爱奥尼亚2 xhr.open('GET', "https://getbytitle('LE_COE_Mapping')/items(2)/AttachmentFiles('3.jpg')/$value"); xhr.responseType = 'blob'; xhr.setRequestHeader("X-Requested-
xhr.open('GET', "https://getbytitle('LE_COE_Mapping')/items(2)/AttachmentFiles('3.jpg')/$value");
xhr.responseType = 'blob';
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
// xhr.setRequestHeader('Authorization', 'Token token="' + token + '"');
xhr.setRequestHeader('Authorization', token);
xhr.onload = function (e) {
var img = new Image();
var url = window.URL || window.webkitURL;
img.src = url.createObjectURL(this.response);
document.getElementById('Doctitle').appendChild(img);
// alert(url.createObjectURL(this.response));
};
xhr.send();
拜托,这两天来我一直被困在这里
请帮帮我。
谢谢有一次我也遇到了同样的问题,然后我就这样做了:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200){
//this.response is your final response
console.log(this.response, typeof this.response);
var img = // your img syntax
//your any custom code goes here
}else{
//show error message if you want to show
}
}
xhr.open('GET', 'http://Your image url');// image url be like http://ab.cd/img.png
xhr.responseType = 'blob';
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
//xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
//send authorisation if you sending token
xhr.send();
希望这能对您有所帮助。我在下面解决了这个问题:
this.array:这是我们传递以获取特定url的blob url的url:
const headers1 = new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': "Bearer " + this.accessToken,
'X-Requested-With': 'XMLHttpRequest'
})
const response = await this.http.get(this.array + "/$value", { headers: headers1, responseType: 'blob' }).toPromise();
let b: any = new Blob([response], { type: 'application/octet-stream' });
var url = window.URL.createObjectURL(b);
console.log("this is url BLOB " + url);
您可以通过转换为base64字符串来实现这一点。看看这个@amku91。请看我更新的问题[ts]找不到名称“handler”。在行处理程序(this.response)中,@user9483522 handler是我的自定义函数。哦,对不起。删除这一行并检查控制台显示的内容。你能告诉我如何显示图像吗,我得到了响应,然后@amku91做什么你可以这样做:var blob=this.response;document.getElementById(“您的图像div id”).src=window.URL.createObjectURL(blob)@用户9483522还可以附加到要显示图像的文档中@用户9483522