简体   繁体   English

在sap.ui.commons.Image中加载备用图像

[英]Loading alternate image in sap.ui.commons.Image

new sap.ui.commons.Image({src: "http://localhost:9090/images/some-image.jpg"});

Suppose the above fails if http returns 404 or some error . 如果http返回404或某些错误,假设上面的方法失败。 The problem is the request is sent by Openui5 internally so it is not possible to intercept from server side either .In that case is there any way to give an alternate src such as below: 问题是请求是由Openui5内部发送的,因此也无法从服务器端进行拦截。在这种情况下,可以通过任何方式提供备用src ,如下所示:

redirect url in case of http error - src:"http://localhost:8080/images/no-image.png" 如果发生HTTP错误,则重定向URL- src:"http://localhost:8080/images/no-image.png"

I have been trying the below but its not giving the result: 我一直在尝试下面的方法,但是没有给出结果:

   $(document).ajaxError(function myErrorHandler(event, xhr, ajaxOptions, thrownError) {
   $.ajax({
        // Resend the req
        url: "http://localhost:8080/images/no-image.png"
        }); 
    //imageFile.setSrc("http://localhost:9090/images/no-image.png");
    }); 

您可以使用setAlt方法设置替代图片(如果找不到)

I have found a solution using formatter function for the source path received: 我找到了使用formatter function接收源路径的解决方案:

new sap.ui.commons.Image({src: {path : 'itemImg',formatter: function(itemImg){
        var ImageObject = new Image();
        ImageObject.src = itemImg;
        if(ImageObject.height>0){
        //alert("Ping worked!");
        return itemImg;
        }else {
        //alert("Ping failed :(");
        return "http://localhost:9090/images/no-image.png";
        }
    }}}).addStyleClass("itemImg"),

Alternate solution as suggested by @Qualiture @Qualiture建议的替代解决方案

ImageObject.onload = function() {
    //alert('image loaded successfully.');
    return itemImg;
};
ImageObject.onerror = function(){
  //alert('image loading failed');
  return "http://localhost:9090/images/no-image.png";
};

Note: Use first solution if you think an image with height 0 is pointless and better to display a default image else the latter is more efficient. 注意:如果您认为height 0的图像毫无意义,最好显示default image否则使用第一个解决方案,否则后者更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM