Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How to determine if image URL actually exists

    I have a store of images related to products in a product table. The URL to the image can be constructed using the product ID but not all products have images. If a product image is missing I want to display a placeholder image instead. My question is, how do I determine whether the URL I construct points to an actual image file.

    #2
    You can use the RPCManager to send an rpcRequest to that URL, with useSimpleHttp:true and the image URL as the actionURL, and check the httpStatusCode on the resulting response.

    Comment


      #3
      It took a little trial and error but I finally got it. The only problem is that a warning alert gets displayed when the file doesn't exist. Here's my code. How do I suppress the warning? I would have thought that setWillHandleError(true) would do that.
      Code:
      itemImg.setSrc("../customimages/" + rec.getAttribute("ISKU") + ".jpg");
      RPCManager.setActionURL(itemImg.getSrc());
      RPCRequest rpcReq = new RPCRequest();
      rpcReq.setUseSimpleHttp(true);
      rpcReq.setWillHandleError(true);
      RPCManager.sendRequest(rpcReq, new RPCCallback(){
      	public void execute(RPCResponse response, Object rawData,
      			RPCRequest request) {
      		if (response.getHttpResponseCode()==404) {
      			itemImg.setSrc("../customimages/noImageAvailable.jpg");
      		}
      	}
      	
      });
      itemImg.redraw();

      Comment


        #4
        What's odd is that it is inconsistent. As I move around between different products that do not have an image file, about half the time it works as expected without showing the Warn alert and about half the time the alert shows up.
        Last edited by jay.l.fisher; 23 Nov 2009, 18:53.

        Comment


          #5
          Kind of sounds like more than one codepath, one where willHandleError is set and one where it's not. You might check the RPC tab in the Developer Console and look at all your requests to verify that you're actually setting willHandleError.

          Comment


            #6
            That is the only code that does anything explicitly with RPC requests. It also has a side effect on other RPC operations. I have another grid in the app where I set filter criteria and fetchData based on user actions. The Warn alert about the transport error for the last image I tried to load starts showing up when the grid data is being fetched. Do I need to do something to "reset" the RPCManager?

            Comment


              #7
              Problem solved. I realized that I was setting the action URL on the RPCManager itself instead of just for the one request. Thanks for the help.

              Comment

              Working...
              X