6 Replies Latest reply on Sep 21, 2006 1:19 AM by Newsgroup_User

    Code before CF writes client file to disk

    mike__j
      Can I write some CF code and <cfflush> it out before CF tries to process form values or at least before it tries to write a client file to disk?

      Here is the situation:
      A form has a file input element so that the client can upload a file. Sometimes it takes a while (several seconds) to upload the file. The majority of the time is waiting for CF to write the file to a temp directory. I'd like to write some CF code that will execute before CF tries to process the form values and puts them into the form scope -- or at a minimum, before it tries to write the client file to disk in the tmp directory. The output I'd like to produce would be an animated gif and some text. Here's an example of the flow I would like to have:

      * Form is submitted by client
      * *** special *** generate some output and flush it out to the client (maybe via an "undocumented" file, 'preFormProcess.cfm' -- similar to 'application.cfm'. This must occur before CF tries to put the form values into the form scope, or at least before it tries to write the file to disk in the tmp directory.
      * Then, have CF do it's normal thing, process the form fields, stream the file to the tmp directory
      * execute the normal cf code

      I can do this in C/C++ because I can determine when I read the form data that was posted (via standard input), but I don't see any way to do this in ColdFusion, because it automatically processes all the form values, and automatically writes any uploaded files to disk, before I can have any of my code execute.

      Please let me know if there is any way to sneak in my code before CF automatically processes the form data.
      Thanks!
        • 1. Re: Code before CF writes client file to disk
          Dan Bracuk Level 5
          You can do exactly what you described in the first paragraph, well, I can. What did you try and what went wrong?
          • 2. Re: Code before CF writes client file to disk
            Mr Black Level 1
            This is not possible regardless of programming language (including C/C++). This is by definition of HTTP protocol. You cannot reply to the client using the same HTTP (TCP) connection until entire client's request is consumed by the server.

            If you want to perform some activities on the client side while HTTP request is processed, you have to do it entirely on the client side (JScript, Flash, ActiveX, Applet, etc) or by utilizing another HTTP connection. This is how upload monitors work.

            Resume: there cannot be any "simple" <cfflush>-like solution.
            • 3. Re: Code before CF writes client file to disk
              mike__j Level 1
              If I have a form, a.cfm, where a file can be uploaded, and posts the data to b.cfm, I can't even <cfflush> 'Hello World.' until the file is completely written to the tmp directory by ColdFusion. This can take 30 seconds or more if the file is big enogh or the connection is slow. Thus, the user sees no change on the screen until their file is totally uploaded. Note that this is done automatically by CF before even 1 line of my code is executed. I want to give the user some response before the file is totally uploaded by CF to the tmp directory. What I'm asking is this: Can I <cfflush> any output before CF writes the entire file to disk?

              I can do this using another language such as 'C', where you can control when you open the input stream and start reading the file. In C, I can flush out some output, in order to give some response to the user, then read the input stream later. In fact, in C, I can totally ignore the input stream if I want to. The problem I'm having is that ColdFusion automatically reads the entire input stream (and makes all the Form variables, etc), before it executes any of my code. What I need to do is have some of my ColdFusion code execute before ColdFusion automatically reads the entire input stream. Is there any way to do this in ColdFusion?

              I know that I can use other client side tricks, like hidden iframes, javaScript and the like to entertain the user while the file is being uploaded. That's not what I'm looking for. I'm looking for a solution to the ColdFusion issue. I hope the description of the problem makes more sense now.
              • 4. Re: Code before CF writes client file to disk
                MikerRoo Level 1
                As already stated, you cannot do this in C or any other language with a simple form.
                Read the html specs starting at http://www.w3.org/TR/html4/interact/forms.html#h-17.13.3.4

                The BROWSER sends all of the file information as part of the post.
                This is not a CF issue. It's how html and all browsers work.

                You must use client side tricks like staggered submit or rolling your own upload client to get any more user feedback.

                If you say you can do this in C, then (1) prove it here and then (2) call that C snippet from CF or the client's browser (problem solved).
                • 5. Re: Code before CF writes client file to disk
                  mike__j Level 1
                  All:

                  Thanks for your responses to this issue. I was able to put together a simple Perl script to show that I can send reply data before any of the input stream from the client is read. The program will actually send the output back once it is called, even though the client is sending data, i.e. a really large file. The server side code completes in less than a second, before the client is even finished sending all of it's data, but the client won't bear any of the fruit of this. The problem is that the client won't receive the reply until it is done sending the data. I now understand the previous comments. I misunderstood, and thought you were saying that the server side program couldn't output anything until the entire request was sent. I get it now.

                  What I'm wondering is this: In Coldfusion, is there any way to read the input stream sent by the client, and handle it as I wish, instead of the CF engine automatically processing the input stream? My guess is that I can't, and that I've have to write a Java program (or some other language) to accomplish this. Just wondering if it can be done in ColdFusion.

                  For reference, here's a simple Perl script where I can read the standard input stream myself -- can something like this be done in CF?

                  #!D:/perl/bin/perl.exe

                  print "Content-type: text/html\n\n";

                  print <<END_HTML;

                  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

                  <html>
                  <head>
                  <title>Untitled</title>
                  </head>

                  <body>

                  This is the post page, post.pl.<br>
                  <pre>

                  END_HTML

                  ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
                  print "Current time= $min:$sec <br>";

                  #sleep(2);

                  # read the request data from standard input
                  #while (defined($line = <STDIN>)) {
                  # process $line here
                  #print "LINE=", $line, ";";
                  #}

                  print <<END_HTML2;

                  </pre>
                  </body>
                  </html>

                  END_HTML2

                  • 6. Re: Code before CF writes client file to disk
                    Level 7
                    All *ColdFusion* does is copy the file from the webserver's temp dir to
                    wherever you tell it to go. Which - given the two servers are usually
                    running on the same physical box - doesn't take very long.

                    CF is *not at all* involved in the upload process between client and web
                    server. In fact CF has nothing *at all* to do with any communications with
                    the client. CF only talks to the web server.

                    If you want there to be any additional activity on the *client side*, then
                    you will have to write something that runs on the client side. So that'd
                    be Javascript, a Java applet or something with Flash (etc). CF does not
                    run on the client.

                    --
                    Adam