Skip to main content
WolfShade
Legend
November 29, 2010
Question

File direct from database to open/save, without tempfile

  • November 29, 2010
  • 2 replies
  • 3688 views

Hello, everyone.

I've got the file uploading to the database, and that is working most excellent.

How do I get it to open/save immediately without first using CFFILE action="write" to save it to a server folder, first?

I'm using the following code:

<cfcontent variable="#getFile.contents#" reset="true"><!--- binary data for file, pulled from "varbinary(max)" --->
<cfheader name="content-disposition" value="attachment;filename=#getFile.fileName#">
<cfheader name="content-type" value="application/octet-stream">
<cfoutput>#toString(getFile.contents)#</cfoutput>
<cfabort>

.. but all this does is open a dialogue asking the user to either open or save the CFM file calling it, but the .cfm is replaced with .ZIP.

The files are not all going to be the same MIMEtype - some will be Word documents, some Excel documents, some PowerPoint documents, and some PDF documents.

^_^

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    November 29, 2010

    Your code seems unnecessarily complex. In the following example, the file p.pdf is in the dir directory on my desktop. I wish to offer it to the client for download, as the file abracadabra.pdf.

    <!--- read the PDF as a binary --->

    <cffile action = "readBinary" file = "C:\Documents and Settings\bkbk\Desktop\x\p.pdf" variable ="pdfBinaryObj">

    <!--- offer the PDF for download --->

    <cfheader name="Content-Disposition" value="attachment;filename=abracadabra.pdf">
    <cfcontent variable="#pdfBinaryObj#" type="application/octet-stream; charset=UTF-8">

    Community Expert
    November 29, 2010

    The point of using the VARIABLE attribute of CFCONTENT is so that you don't have to explicitly output the value later on. Here's an example:

    http://awads.net/wp/2006/01/25/savingdownloading-files-tofrom-oracle-using-coldfusion/

    (scroll down to the part of the page that discusses the output of the content)

    As far as the prompt that the user receives, you can't really do anything about that. The user has to choose whether to download the file at all, and whether to open or save the downloaded file.

    Dave Watts, CTO, Fig Leaf Software

    http://www.figleaf.com/

    http://training.figleaf.com/

    Fig Leaf Software is a Veteran-Owned Small Business (VOSB) on

    GSA Schedule, and provides the highest caliber vendor-authorized

    instruction at our training centers, online, or onsite.

    Read this before you post:

    http://forums.adobe.com/thread/607238

    Dave Watts, Eidolon LLC
    WolfShade
    WolfShadeAuthor
    Legend
    November 29, 2010

    Sorry.. I didn't make myself clear.  As far as the prompting to either save or open the file, that's what I expect.  What I wasn't expecting was the fact that the file that is prompted to open or save is the name of the ColdFusion document that is trying to execute.  (ie, the CFM document is called "details_supplier_docs_open.cfm" and when executed it prompts the user to open or save "details_supplier_docs_open.cfm" instead of "this_requested_excel_file.xlsx".)

    Inspiring
    November 29, 2010

    I think there may a few problems. But the point about cfcontent stands. You are using it incorrectly. Take a look at the example in the link. Notice how the cfheaders are positioned first, then the cfcontent?