Skip to main content
WolfShade
Legend
February 8, 2011
Question

Another file upload question

  • February 8, 2011
  • 1 reply
  • 706 views

Hello, everyone.

Is there a way to insert an image into a database WITHOUT having to save it to the server HD, first?

For example, is it possible to use a form to allow a user to select an image, upload it to server memory, use CFFILE to read it into a binary variable, then insert that directly into the database, completely skipping the part where I have to save it to the server HD and readbinary from that?

Thanks,

^_^

    This topic has been closed for replies.

    1 reply

    ilssac
    Inspiring
    February 8, 2011

    WolfShade wrote:

    Is there a way to insert an image into a database WITHOUT having to save it to the server HD, first?

    Nope, I'm afraid there is not.

    Because by the time ColdFusion is working with the file, the file has already been uploaded and written to the server hard driver by the web server application.

    The following is the process by which a file is uploaded through a client browser to a web server.

    1) User selects a file with a HTML file control rendered in a browser.

    2) The browser reads the file from the client system and encodes it into the header of a HTTP request.

    3) The browser sends the HTTP request to the web server.

    4) The web server application reads the encoded file in the header of the HTTP request, decodes it and writes it to a temporary file location.


    (assuming that the requested URI resource is a ColdFusion template)

    5) The web server hands the request to ColdFusion telling CF that there was a file uploaded and the file path to the temporary location.

    6) CFML <cffile action="upload"....> that is normally ran on uploaded files will copy the file from the temporary location to the desired file location on the server.

    As you can see, there is little you can do to stop the file from being written to the server hard drive.

    What you CAN do, is fetch the file directly from the temporary location and not move it somewhere else with the <cffile action="upload"...> tag.  If you look at the data in the form.uploadFileField form field, you will see that it is a path to the temporary file location where the web server put the file.  You can directly read this file with a <cffile action="read"...> tag.

    Just realize that doing this you will not have a convenient structure of data associated with the uploaded file, such as the original file name, the server file name, etc.  Some of that must be available in the http headers, but I have never bothered with finding it.

    Not sure if that provides much benifit, but it can be done.

    WolfShade
    WolfShadeAuthor
    Legend
    February 8, 2011

    Thanks, ilssac.  I knew that it wrote to a temp location, and it is at this point where I want to then read the file into a binary variable and load the database with that instead of using CFFILE to save it to the HD.  If CF acts the same way PHP does, once that file is written to the temp folder if it is not acted upon immediately it is lost/deleted.

    I've put a CFDUMP on the document attempting to get the value of the file upload field, but the field isn't even showing up in the CFDUMP, so I can't even get the temp location/filename.  Not sure why it's working that way.

    ^_^

    ilssac
    Inspiring
    February 8, 2011

    <cfdump var="#form#"> does not show the file field information?

    How about <cfoutput>#form.fileUploadField#</cfoutput>

    I know it is there.  I've played with this once before for a somewhat similar reason as you are looking.  But I abondended it when I realized that going directly to the temp location puts all the work on me to figure out the orginal uploaded file name and other information.