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 thoughts on “Handling file uploads without CFFILE

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>