سؤال

When using coldfusion to store files (can be .txt, .doc, .docx), I want to be able to retrieve these files. I've been googling for a while but I can't seem to find an answer.

Basically: how do I retrieve a blob (varbinary(MAX) in SQL Server 2008) that can be numerous types of file extensions, and then offer a download?

Here is my upload/download code, of which the download has me stumped:

<!--- <form action="resume.cfm" method="post" enctype="multipart/form-data">
    Select File: <input type="file" name="upload" />
    <input type="submit" value="Upload File" />
</form>

<cfif structKeyExists(form, "upload")>
    <cfquery datasource="#application.dsn.recAppTest#" name="resume">
    INSERT INTO resume (resume)
    VALUES(
        <cfqueryparam cfsqltype="cf_sql_blob" value="#FileReadBinary(FORM.upload)#">
    )
    </cfquery>
</cfif> --->

<cfoutput>
    <cfquery datasource="#application.dsn.recAppTest#" name="resume">
        SELECT * FROM resume
    </cfquery>
    <cfdump var="#resume#" />
</cfoutput>

<cfheader name="Content-Disposition" value="attachment; filename=""> <!--- what goes here if I don't know the incoming extension?? --->

<cfcontent type="text/plain, application/vnd.openxmlformats-officedocument.wordprocessingml.document" />

I'm not even sure if this is possible (I hope it is), but if not, would a workaround be to (on upload) grab the file extension before it's converted into numbers (varbinary) and store it as a separate field?

هل كانت مفيدة؟

المحلول

As @imthepitts says, you need to store the filename when you receive the document. <cfcontent> supports a var attribute which allows you to send back the file in one operation:

<cfcontent type="application/...." var="#resume.resume#" />

Make sure you check your code works with your webserver in place. The JRun built-in server sends back slightly different headers and responses than either IIS or Apache will.

If earlier versions of IE need to be supported, do test them. I've had issues where the order of the headers made a difference (even though it shouldn't). I've had to drop down to using the underlying HTTPServletResponse methods. This link, although very old is useful.

نصائح أخرى

Here is the finalized code:

Notes:

BLOB must be checked in cfadmin or else you'll receive a truncated result.

Live code: inserts file into sql server as varbinary data

Commented code: retrieves file (manipulate the query as needed of course) and offers a download

<form action="resume.cfm" method="post" enctype="multipart/form-data">
    Select File: <input type="file" name="upload" />
    <input type="submit" value="Upload File" />
</form>

<cfif structKeyExists(form, "upload")>
    <cfset destination = expandPath("./resumes")>
    <cfif not directoryExists(destination)>
        <cfdirectory action="create" directory="#destination#">
        </cfif>
          <cffile action="upload" 
            filefield="upload" 
            destination="#destination#" 
            nameConflict="makeUnique" 
            result="resume" 
          accept="text/plain, application/vnd.openxmlformats-officedocument.wordprocessingml.document">
    <cfdump var="#resume#" />
    <cfquery datasource="#application.dsn.recAppTest#" name="queryresume">
    INSERT INTO resume (resume, filename, ext)
    VALUES(
        <cfqueryparam cfsqltype="cf_sql_blob" value="#FileReadBinary(FORM.upload)#">,
        <cfqueryparam cfsqltype="cf_sql_varchar" value="#resume.serverFileName#">,
        <cfqueryparam cfsqltype="cf_sql_varchar" value="#resume.serverFileExt#">
    )
    </cfquery>
    <cfset xfile="#resume.serverDirectory#\#resume.serverFile#" />
    <cfdump var="#xfile#" />
    <cffile action="delete" file="#xfile#">
</cfif>

<!--- <cfoutput>
    <cfquery datasource="#application.dsn.recAppTest#" name="resume">
        SELECT * FROM resume
    </cfquery>
    <cfdump var="#resume#" />
</cfoutput>

<cfheader name="Content-Disposition" value="attachment; filename=#resume.filename#.#resume.ext#">

<cfcontent variable="#resume.resume#" reset="true" type="text/plain, application/vnd.openxmlformats-officedocument.wordprocessingml.document" /> --->
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top