7 Replies Latest reply on Apr 17, 2008 2:17 PM by larry_schwartz

    Insure file exists before displaying

    larry_schwartz
      I have JPGs stored in BLOBs. I have several lines of code to retrieve the JPG put it on the server. The code to download the BLOB ocurrs right before the IMG tag used to display it in my CFM page. Is there is a way to make sure the code to retrieve/download the BLOB has finished executing (i.e., the JPG has made it to the server) before the IMG tag tries to display the JPG?

      To ask a different way -- Is there a way to have CF "stand still" until the desired lines of code have finished executing?
        • 1. Re: Insure file exists before displaying
          Level 7
          larry_schwartz wrote:
          > To ask a different way -- Is there a way to have CF "stand still" until the
          > desired lines of code have finished executing?
          >

          Well there are ways to put a thread to sleep with ColdFusion. In CF8
          there are tags|functions to control this, for previous versions you will
          need to create and call a Java object. A quick Google would show you
          the code.

          But you do not need to do that in this case.

          ColdFusion is NOT going to ever process the <img...> tag. That
          is an HTML tag, not CFML tag and it is going to be processed by a
          browser on the client. The client browser does not get the response
          until ColdFusion is completely done building the entire response and
          sends it to the client.

          In fact, by the time the browser has got the request and is requesting
          that <img...> file, ColdFusion has moved on and is working on a
          completely new request.

          • 2. Re: Insure file exists before displaying
            larry_schwartz Level 1
            I'm not explaining myself properly.

            I'm in CF8.

            I've done Google searches, but either I'm missing an answer that's in my face or I don't see how to do what I need.

            To better explain my situation:

            I have a CFM template.

            It has an IMG tag. It refers to a JPG witha generiic name that does not yet exist.

            In the CFM template, right before the IMG tag, is code to: Hit the database and retrieve a BLOB, then write that BLOB with the generic filename to the place the IMG tag refers to.

            There *seems* to be some miscoordination going on. Maybe not, but it seems so. It seems the HTML is being served to the browser before the file is completely retrieved and written to disk.

            So, I'm wondering if there's a way to keep CF from "moving on" and serving the HTML to the browser until it knows the file is retrieved and written to disk.

            Or, does CF already do this? Meaning my problem lies elsewhere.

            Thanks.
            • 3. Re: Insure file exists before displaying
              edgriffiths
              is there a reason why the JPG filename has to be generic?

              if not, why not point the IMG tag at a CFM script that:
              1. retrieves the JPG BLOB data from the DB
              2. uses CFCONTENT to return the BLOB data as JPG mimetype?

              <img src="myJpgReader.cfm?imgID=12345">
              • 4. Re: Insure file exists before displaying
                Level 7
                larry_schwartz wrote:
                >
                > So, I'm wondering if there's a way to keep CF from "moving on" and serving the
                > HTML to the browser until it knows the file is retrieved and written to disk.
                >

                As I mentioned there are ways to put the current thread to sleep in
                ColdFusion. IIRC, CF8 has tags or functions to do this now. If not, or
                for earlier versions of CFMX, you can use a Java thread object to sleep
                the thread.

                Also you could create a file watcher that confirms that the file is
                completely written before moving on. Search for an article titled
                "Directory Watcher Dangers - A Follow-Up." It spells out how to create
                a while loop that will wait until I file is completely written before
                moving on with processing.

                >
                > Or, does CF already do this? Meaning my problem lies elsewhere.

                That would be my first thought. I would think that since ColdFusion is
                going to completely process the entire response before sending anything
                to the browser. Then the browser is going to have to render the
                received response to find the <img...> tag. Then it makes a new request
                to get the <img...> file. I would think that this file would long be
                written and ready and waiting.

                You say you are using a generic name? Is this name getting reused
                and|or replaced from request to request? If you follow the implications
                of the above data flow, you can't easily reuse a 'generic' file name
                that is shared between many requests without a strong possibility for
                data sharing and|or race conditions. Could this be part of the problem?
                • 5. Re: Insure file exists before displaying
                  larry_schwartz Level 1
                  Hi,

                  Yes, the name needs to be generic. The filepath is not (it changes).

                  Intersting idea in using the template as the IMG tag source. Will try that.

                  Thanks.
                  • 6. Re: Insure file exists before displaying
                    Level 7
                    if you are on cf8, why not use <cfimage action='writetobrowser' ...> ?

                    Azadi Saryev
                    Sabai-dee.com
                    http://www.sabai-dee.com/
                    • 7. Re: Insure file exists before displaying
                      larry_schwartz Level 1
                      Thanks, everyone, for the suggestions and help. Turns out it was a series of small errors, not the problem I thought it was. I appreciate the information, I'm sure it'll help in the future.

                      Long story about not using CFIMAGE/WriteToBrowser, I had to use the IMG tag in this situation due to a client requirement.