Angular 使用动态路径在新窗口中查阅图像
我正在尝试显示具有动态路径的图像 在component.html中,我有: 通过使用静态方式1,我可以显示我的图片 但是,当使用DynamicWay2时,我什么也没有得到,因为我知道path与../assets/upload/semone.jpg具有相同的值 我得到 错误{…} 错误:Blob{size:192,type:text/html} 标题: 对象{normalizedNames:Map0,lazyUpdate:null,lazyInit: lazyInit} 消息:的Http失败响应 : 404找不到 名称:HttpErrorResponse 好:错 现状:404 statusText:未找到 网址: : {…} 如果我关闭terminal node.js并用命令ng serve重新启动它,我可以用动态路径显示图像Angular 使用动态路径在新窗口中查阅图像,angular,spring-boot,Angular,Spring Boot,我正在尝试显示具有动态路径的图像 在component.html中,我有: 通过使用静态方式1,我可以显示我的图片 但是,当使用DynamicWay2时,我什么也没有得到,因为我知道path与../assets/upload/semone.jpg具有相同的值 我得到 错误{…} 错误:Blob{size:192,type:text/html} 标题: 对象{normalizedNames:Map0,lazyUpdate:null,lazyInit: lazyInit} 消息:的Http失败
你对解决那个问题有什么想法吗?。非常感谢。使用资产文件夹是静态使用的,在动态情况下这不是解决方案,因为ng serve将应用程序加载到内存中,这就是为什么最新添加的项目,比如图像、pdf。。。无法在当前编译的应用程序中加载 例如,解决方法是引用RESTAPI生成的路径http://localhost:6227/api/auth/files/img1.jpg 希望对你有所帮助
<span (click)='showFile()'>Consult</span>
showFile()
{
var req= new XMLHttpRequest();
req.open('GET', 'http://localhost:6227/api/auth/users/' + this.usrId, false);
req.send(null);
this.connectedUser= JSON.parse(req.responseText);
// let path = '../assets/upload/semone.jpg'; // Static way (1)
let path = this.connectedUser.pathPic; // Dynamic way (2)
this.http.get(path, {observe: 'response', responseType: 'blob'}).pipe(map(res =>
{
return new Blob([res.body], {type: res.headers.get('Content-Type')});
})).subscribe(hi =>
{
const xx = URL.createObjectURL(hi);
window.open(xx);
});
}