27
Jun
03

Handling file uploads without CFFILE

Many of us don’t have the luxury of dedicated servers and have to live with a shared host. Since hosts often limit some CF tags for security reasons, this appears to limit what we can do in CF. This question has come up a number of times in the forums so I put together an example of how to handle file uploads without CFFILE.

The following code block displays a very short form that allows a user to uploda a file. Once the file is uploaded it reads it in line by line and calls a function on each line. While all I do is output the file line by line to the client, the function could just as easily parse out the line and insert to a database.


      s = "";
      cls = s.getClass();

      stringClass = cls.forName("java.lang.String");

      fileReaderClass = cls.forName("java.io.FileReader");

      a = arrayNew(1);
      a[1] = stringClass;

      fileReaderConst = fileReaderClass.getConstructor(a);

      a[1] = form.upFile;

      fileReader = fileReaderConst.newInstance(a);

      readerClass = cls.forName("java.io.Reader");
      a[1] = readerClass;

      bufferedReaderClass = cls.forName("java.io.BufferedReader");
      bufferedReaderConst = bufferedReaderClass.getConstructor(a);

      a[1] = fileReader;
      bufferedReader = bufferedReaderConst.newInstance(a);

      try {
         do {
            s = bufferedReader.readLine();
            processLine(s);
         } while (true);
      } catch (coldfusion.runtime.UndefinedVariableException e) {
         // this indicates end of file, ok to ignore error
      } 

#arguments.line##chr(13)##chr(10)#

If you’re interested in following the webforums thread you can view it here.

Of course this is CFMX only since it takes advantage of the fact that CFML compiles down to Java. I originally picked the technique up from Matt Liotta’s post on the CFCDev mailing list.


7 Responses to “Handling file uploads without CFFILE”


  1. 1 effects of propalene diet pill Oct 29th, 2004 at 12:45 pm

    Hello there,

    Iwas browsing the web and found this blog. Some interesting quotes. Keep them coming!

    Alice
    effects of propalene diet pill

  2. 2 secured loan Nov 2nd, 2004 at 6:02 pm

    Hello there,

    Iwas browsing the web and found this blog. Some interesting quotes. Keep them coming!

    Alice
    secured loan

  3. 3 secured loan Nov 2nd, 2004 at 6:02 pm

    Hello there,

    Iwas browsing the web and found this blog. Some interesting quotes. Keep them coming!

    Alice
    secured loan

  4. 4 Max Power Dec 3rd, 2004 at 7:46 am

    Welcome Home Everyone Home

    Cya
    Max

  5. 5 Leicester Call Centre Dec 3rd, 2004 at 10:02 pm

    Hi Guys

    Thanks for taking the time posting. MSN have finally launch blogs, what was with the delay?
    Leicester Call Centre

    Cya
    Bruce

  6. 6 Caiek Feb 3rd, 2007 at 8:54 am

    So much spam (

  7. 7 Abigail Feb 3rd, 2007 at 11:33 am

    COol :)

Comments are currently closed.