【问题标题】:Display an image by clicking on button通过单击按钮显示图像
【发布时间】:2020-03-16 18:13:18
【问题描述】:

单击按钮后,我正在尝试在新窗口上显示图像。

但是,有些时候,这是无法做到的。

Component.ts

displayMyImage()
    {
    this.http.get(this.imagePath, {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);
            });
}

Component.html

<span (click)='displayMyImage()'>Display</span> 

<button (click)="displayMyImage()">Display</button>

我收到了that exception

你能告诉我我错过了什么吗?非常感谢。

【问题讨论】:

  • 这能回答你的问题吗? Open image in new window
  • 您好@ChintanJoshi 先生,非常感谢您的重播。我尝试了您提出的线程的正确答案,但知道 largeImage 的值是../assets/upload/Ordinaryespdsi2019o4f8dh1584349128317.jpg,这对我不起作用。请问您有解决这个问题的任何想法吗?非常感谢。
  • 你的情况下 imagePath 是什么?
  • 如果我使用 staticPath 作为&lt;img id="staticPath" src="../assets/upload/Ordinaryespdsi2019o4f8dh1584349128317.jpg" style="max-width:100px"&gt; --> 效果也很好。但是如果我使用 dynamicPath&lt;input id='dynamicPath' type="text" value="{{saria2020()}}"&gt; --> 那不起作用,知道 saria2020 的值是 ../assets/upload/Ordinaryespdsi2019o4f8dh1584349128317.jpgcomponent.ts 上就像 saria2020() : String{return this.specialUser.displayPathImage;}

标签: angular spring-boot


【解决方案1】:

也许这样会有所帮助。

document.getElementById('bigpic').style.display='block';

【讨论】:

    【解决方案2】:

    您可以声明如下函数来获取图像预览。我想你那里有 imagePath。

    displayMyImage() {
    
        var imagePath = "https://www.howtogeek.com/wp-content/uploads/2018/06/shutterstock_1006988770.png";
        window.open(
          imagePath,
          "Image",
          "width=largeImage.stylewidth,height=largeImage.style.height,resizable=1"
        );
    }
    

    希望这会有所帮助。

    【讨论】:

    • 如果我使用 staticPath 作为&lt;img id="staticPath" src="../assets/upload/Ordinaryespdsi2019o4f8dh1584349128317.jpg" style="max-width:100px"&gt; --> 效果也很好。但是,如果我像 &lt;input id='dynamicPath' type="text" value="{{saria2020()}}"&gt; 这样使用 dynamicPath --> 那不起作用,知道 saria2020 的值是 ../assets/upload/Ordinaryespdsi2019o4f8dh1584349128317.jpgcomponent.ts 上就像 saria2020() : String{return this.specialUser.displayPathImage;}
    • 所以我的问题是动态路径。我不知道如何解决这个问题?。
    • 您是获取动态路径作为 URL 还是静态路径?
    • 仍然无法正常工作,先生,使用 静态路径,我可以使用 url localhost:4300/assets/upload/Ordinaryespdsi2019o4f8dh1584349128317.jpg 显示图像,但使用带有 {{saria2020} 的 dynamicPath } 而不是 {{saria2020()}} 我没有得到任何 url localhost:4300/null
    • dynamicPath id 是动态路径。这是saria2020() : String{return this.specialUser.displayPathImage;} 的结果,即../assets/upload/Ordinaryespdsi2019o4f8dh1584349128317.jpg
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 2012-04-01
    相关资源
    最近更新 更多