【发布时间】:2020-03-30 19:25:52
【问题描述】:
我需要从 backend/images 文件夹中获取图像作为 component.ts 文件中的 Image 对象。我可以从数据库中获取图像的路径。
this.productsService.getProduct(this.editId).subscribe(productData => {
this.name = productData.name;
this.code = productData.code;
this.category = productData.category;
this.description = productData.description;
this.details = productData.details;
this.editImagePaths = productData.imagePaths;
});
我尝试通过 http 请求获取图像。
for (let i = 0; i < this.editImagePaths.length; i++) {
this.http.get<File>(this.editImagePaths[i]).subscribe(value => {
this.images.push(value);
});
}
images 是一个文件类型的数组。问题是 http.get 返回 blob 字符串,而 core.js 给出以下错误;
core.js:6014 错误 HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:3000/images/asd0-1575503057766.png", ok: false, …} 错误:{error: SyntaxError: Unexpected token � in JSON at JSON.parse () at XMLHtt...,文本:“�PNG ↵↵ IHDRI������PLTE�F���+*)…�LЙ�3 @������������y�(pIEND�B`�"} 标头:HttpHeaders {normalizedNames:Map(0),lazyUpdate:null,lazyInit:ƒ} 消息:“http://localhost:3000/images/asd0-1575503057766.png 解析期间的 Http 失败” 名称:“HttpErrorResponse” 好的:假的 状态:200 状态文本:“确定” 网址:“http://localhost:3000/images/asd0-1575503057766.png” 原型:HttpResponseBase
【问题讨论】:
-
您的内容类型可能是
application/json,而不是image/png。尝试用邮递员或其他方式来缩小你的问题。 (把 angular/HttpClient 去掉)
标签: node.js angular typescript express