2 Replies Latest reply on Sep 11, 2007 10:16 AM by jhandfield

    Displaying Word Document from a Database

    NettlesD Level 1
      Let me try to explain this.

      I can upload and store a Word Document in an Oracle database (BLOB column) but now I need to extract that Word document and place a pointer to it on the Web. Can someone explain to me how this is done?

      I'm trying to do something like have an image displayed on the Web and when you click on it the Word Document is opened. Follow me on that? So, if there are 15 Word documents in the database, then the web page would have 15 images all pointing to each separate Word document.

      Has anyone done this?
        • 1. Re: Displaying Word Document from a Database
          Did you find a way to do this?
          • 2. Re: Displaying Word Document from a Database
            Don't take my word on this, but I think this should work; I've done this in the past to send dynamically-generated CSV files to the user, which is in a way (at least at the perspective of what's being outputted to the user) the same thing.

            First, obviously, you need to retrieve the Word document data from the database. We'll say you put that in a variable called 'wordDoc'.

            <cfset wordDoc = someQuery.wordDataObj />

            Next, we need to set the 'Content-disposition' HTTP header, to tell the client's browser that we're sending a file, and that we want you to call it somefile.doc by default. We'll do this with the <cfheader> tag (note that if you use <cfflush> in the application, this tag (and thus, this approach) will not work.

            <cfheader name="Content-disposition" value="attachment; filename=somefile.doc" />

            Almost there. Now we need to set the Content-type HTTP header, to tell the browser what kind of data is coming, and then give it the actual file data. We'll use the <cfcontent> tag to do both of these things.

            <cfcontent type="application/msword" reset="yes">#wordDoc#</cfcontent><cfabort>

            If memory serves, this should be all you need. Note the <cfabort> after the closing <cfcontent> tag, I've had problems in the past with additional output on the page after these tags being appended to what gets sent to the browser, so I use a <cfabort> afterwards to make sure nothing more gets sent after the data we want. Also, at least with CFMX7, I've noticed that debugging output tends to get added in too regardless, so I typically put a <cfsetting showdebugoutput="false" /> at the top of the file, too.

            I can't guarantee this will work, I haven't tested this particular situation, but if it doesn't, it ought to be close. Let me know if I'm wrong.

            Attached is what should be the code without my commentary / rambling interspersed.