murp

Force file download for Blob files from DB ColdFusion

Mar 2nd, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!---Images in a blob field in the database --->
  2. <cfset imageMgrObj = createObject("component","model.imagetestDAO") .init()/>
  3.  
  4. <!--- Where the number 52 is could be a RC variable for ColdBox or FW/1 --->
  5. <cfset variables.q = imageMgrObj.Read(ID=52) />
  6.  
  7. <cfset variables.myfile = q.largeimage />
  8.  
  9. <cfswitch expression="#LCase(ListLast(q.nameimage, "."))#">
  10.   <cfcase value="avi">
  11.     <cfset variables.contentType = "video/x-msvideo" />
  12.   </cfcase>
  13.   <cfcase value="doc">
  14.     <cfset variables.contentType = "application/msword" />
  15.   </cfcase>
  16.    <cfcase value="docx">
  17.     <cfset variables.contentType = "application/msword" />
  18.   </cfcase>
  19.   <cfcase value="exe">
  20.     <cfset variables.contentType = "application/octet-stream" />
  21.   </cfcase>
  22.   <cfcase value="gif">
  23.     <cfset variables.contentType = "image/gif" />
  24.   </cfcase>
  25.   <cfcase value="jpg,jpeg">
  26.     <cfset variables.contentType = "image/jpg" />
  27.   </cfcase>
  28.   <cfcase value="mp3">
  29.     <cfset variables.contentType = "audio/mpeg" />
  30.   </cfcase>
  31.   <cfcase value="mov">
  32.     <cfset variables.contentType = "video/quicktime" />
  33.   </cfcase>
  34.   <cfcase value="mpe,mpg,mpeg">
  35.     <cfset variables.contentType = "video/mpeg" />
  36.   </cfcase>
  37.   <cfcase value="pdf">
  38.     <cfset variables.contentType = "application/pdf" />
  39.   </cfcase>
  40.   <cfcase value="png">
  41.     <cfset variables.contentType = "image/png" />
  42.   </cfcase>
  43.   <cfcase value="ppt">
  44.     <cfset variables.contentType = "application/vnd.ms-powerpoint" />
  45.   </cfcase>
  46.   <cfcase value="wav">
  47.     <cfset variables.contentType = "audio/x-wav" />
  48.   </cfcase>
  49.   <cfcase value="xls">
  50.     <cfset variables.contentType = "application/vnd.ms-excel" />
  51.   </cfcase>
  52.   <cfcase value="zip">
  53.     <cfset variables.contentType = "application/zip" />
  54.   </cfcase>
  55.   <cfdefaultcase>
  56.     <cfset variables.contentType = "application/unknown" />
  57.   </cfdefaultcase>
  58. </cfswitch>
  59.  
  60. <cfheader name="Content-disposition" value="attachment;filename=test.#LCase(ListLast(q.nameimage, "."))#" />
  61. <cfcontent type="#variables.contentType#" variable="#variables.myfile#" />
Add Comment
Please, Sign In to add comment