2013-09-25 56 views
2

當使用Coldfusion存儲文件(可以是.txt,.doc,.docx)時,我希望能夠檢索這些文件。我一直在搜索Google,但似乎無法找到答案。Coldfusion下載文件

基本上:我如何檢索一個blob(SQL Server 2008中的varbinary(MAX)),它可以是多種類型的文件擴展名,然後提供下載?

這裏是我的上傳/下載代碼,它的下載已經難倒我:

<!--- <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" /> 

我甚至不知道這是可能的(我希望是),但如果沒有,一會解決方法是(在上傳時)抓取文件擴展名,然後將其轉換爲數字(varbinary)並將其存儲爲單獨的字段?

+7

你必須存儲文件名稱,擴展名和上傳文件時的類型。你可以使用'CFFILE'來獲取這些信息。否則,你將不得不猜測:http://www.bennadel.com/blog/2490-Detecting-File-Type-Using-Magic-Numbers-In-ColdFusion.htm – imthepitts

回答

1

正如@imthepitts所說,您需要在接收文檔時存儲文件名。 <cfcontent>支持var屬性,它可以讓你發回的文件在一個操作:

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

請務必檢查你的代碼工作與地方你的網絡服務器。 JRun內置服務器返回的信息與IIS或Apache的報頭和響應略有不同。

如果需要支持早期版本的IE,請測試它們。我遇到了問題,頭文件的順序有所不同(即使它不應該)。我不得不放棄使用基礎的HTTPServletResponse方法。 This link,雖然很古老是有用的。

+0

我把這個標記爲答案,因爲var的屬性正是我所需要的,但是我發佈自己的答案與代碼(當然沒有標記爲答案)我寫了 –

0

這裏是定型的代碼:

注:

BLOB必須cfadmin進行檢查,否則你會收到一個被截斷的結果。

直播代碼:插入文件導入到SQL Server作爲varbinary數據

註釋代碼:檢索文件(如操縱當然需要查詢),並提供了下載

<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" /> ---> 
+2

僅供參考,請務必閱讀[使用ColdFusion進行安全文件上傳的提示](http:// www .petefreitag.com/item/701.cfm),因爲MIME類型可以很容易僞造。 – Leigh

+0

@Leigh,你真棒。你給我的這些提示和答案對於我的學習和應用的進展非常有幫助。對此,我真的非常感激! –

+0

很高興我能幫忙:) – Leigh