我有一个 API 端点,它提供具有以下响应标头的图像:
Server: Apache/2.4.18 (Ubuntu)
X-Powered-By: Express
Access-Control-Allow-Origin: *
Accept-Ranges: bytes
Cache-Control: public, max-age=0
Content-Type: image/jpeg
Content-Length: 445414
当我使用浏览器或 Insomnia 访问此 API 端点时,它可以正确显示图像。但是,当我尝试使用 @awesome-cordova-plugins/http 在我的 Ionic Angular 应用程序中获取此图像时,我收到的响应为空。这是 HTTP 调用返回的响应:
{
"status": 200,
"headers": {
"content-length": "445414",
"server": "Apache/2.4.18 (Ubuntu)",
"x-android-selected-protocol": "http/1.1",
"x-android-response-source": "NETWORK 200",
"access-control-allow-origin": "*",
"keep-alive": "timeout=5, max=100",
"x-powered-by": "Express",
"connection": "Keep-Alive",
"content-type": "image/jpeg",
"accept-ranges": "bytes",
"cache-control": "public, max-age=0"
},
"data": {}
}
数据应包含我需要转换为 base64 的 blob。之前可以用。
这是用于获取它的 Angular 代码:
const reqOptions: any = {
method: "get",
responseType: "blob",
};
const res: HTTPResponse = await this.http.sendRequest(url, reqOptions);
const convertBlobToBase64 = (blob: Blob) =>
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onerror = reject;
reader.onload = () => {
resolve(reader.result);
};
reader.readAsDataURL(blob);
});
const base64Data = (await convertBlobToBase64(blob)) as string;
有什么建议吗?
您指定了responseType:“blob”,但返回的数据字段是一个空对象,这似乎没有将响应解释为blob。尝试使用“text”或“arraybuffer”代替。
如果这不起作用,请尝试通过 Postman 是否有效。