title: Download image from Website Using Coldfusion
body: <p>I am fairly new to ColdFusion. I have the following code that displays the images on the page that I reference on a tutorial which works fine. </p>
  
  <p>When the user selects on any of the images, it will display the image in the browser. However, I would like to allow the user to download the images, rather than display them in the browser.</p>
  
  <p>I am unable to use the HTML5 <em>download</em> to achieve what I am doing, due to the fact that the site I am working on is running off on XHTML 1.0.</p>
  
  <p>I found the following approach <a href="https://www.trunkful.com/index.cfm/2010/8/4/Serving-File-Downloads-with-ColdFusion" rel="nofollow noreferrer">File Downloads with ColdFusion</a> but I am not understanding how to apply it. If anyone could guide me to the right direction or show me another approach, I would appreciate it.</p>
  
  <p>Thank You in advance.</p>
  
  <pre><code>&lt;cfset filters = "*.pdf|*.zip|*.doc*|*.docx*|.ppt*|*.pps*|*.pot*|*.dot*|*.xls*|*.xlsx*|*.swf|*.rtf|*.png|*.bmp|*.jpeg|*.gif"&gt;
  &lt;cfdirectory 
      directory="c:\Downloads\Logos" 
      name="myDirectory" 
      action = "list" 
      filter="#filters#"
      sort="name ASC, size DESC"
      &gt;
  &lt;table width="80%" border="0" align="left"&gt;
      &lt;cfoutput query="myDirectory"&gt;
          &lt;cfset FileExt=ListLast(Name,".")&gt;
          &lt;cfif FileExt eq 'pdf'&gt;
              &lt;cfset imgicon = 'pdf-icon-24.png'&gt;
              &lt;cfelseif FileExt eq 'xlsx' or FileExt eq 'xls'&gt;
              &lt;cfset imgicon = 'Microsoft-Excel-icon-24.png'&gt;
              &lt;cfelseif FileExt eq 'docx' or FileExt eq 'doc'&gt;
              &lt;cfset imgicon = 'docx-icon-24.png'&gt;
              &lt;cfelseif FileExt eq 'lnk'&gt;
              &lt;cfset imgicon = 'url-icon-24.png'&gt;
              &lt;cfelseif FileExt eq 'png'&gt;
              &lt;cfset imgicon = 'png-icon.png'&gt;
              &lt;cfelse&gt;
              &lt;cfset imgicon = 'Documents-icon-24.png'&gt;
          &lt;/cfif&gt;
          &lt;tr&gt;
              &lt;td width="30"&gt;&amp;nbsp;&lt;/td&gt;
              &lt;td  class="e"&gt;&lt;img src="images/#imgicon#" alt="#Name#" class="float-left"/ &gt;&lt;/td&gt;
              &lt;td  class="e"&gt; &lt;a href="ForceDownload.cfm?filename=#EncodeForURL(Name)#"&gt;#Name#&lt;/a&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/cfoutput&gt;
  &lt;/table&gt;
  &lt;/p&gt;
  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;     
  &lt;p class="post-footer align-left"&gt;
      &lt;span class="date"&gt;
          &lt;cfoutput&gt;#dateformat(Now(),"mmm dd,yyyy")#&lt;/cfoutput&gt;
      &lt;/span&gt;
  &lt;/p&gt;
  &lt;br /&gt;
  &lt;br /&gt;      
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Update</strong> I created another script and called it ForceDownload.cfm and did the following:</p>
  
  <pre><code>&lt;cfset folder = "c:\path\Downloads\Logos\"&gt;
  &lt;cfif StructKeyExists(url,"filename") &amp;&amp; fileExists(folder &amp; filename)&gt;
      &lt;cfset filename = href="ForceDownload.cfm?filename=#Name#"&gt;
      &lt;cfset fileInfo = GetFileInfo(folder &amp; filename)&gt;
      &lt;cfset mimeType = getPageContext().getServletContext().getMimeType(folder &amp; filename)&gt;
      &lt;cfheader name="Content-Disposition" value="attachment; filename=""#filename#"""&gt;
      &lt;cfheader name="Expires" value="#Now()#"&gt;
      &lt;cfheader name="Content-Length" value="#fileInfo.size#"&gt;
      &lt;cfcontent type="#mimeType#" file="#folder##filename#" deletefile="No"&gt;
  &lt;/cfif&gt;
  </code></pre>
  
  <p>I replaced the following a tag <code>&lt;a href="Logos\#Name#" &gt;#Name#&lt;/a&gt;</code> with the following a tag <code>&lt;a href="ForceDownload.cfm"&gt;#Name#&lt;/a&gt;</code></p>
  
  <p>When I did that, there was no errors. However, when I click on any of the images, it goes to a blank page in which does not display the image.</p>
  
  <p>I am not sure how to pass the name of the files to filename</p>
  
  <p>Any help would be appreciated.</p>
  
  <p><strong>The Error that is showing</strong>
  <a href="https://i.stack.imgur.com/BXu8Y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BXu8Y.png" alt="enter image description here"></a> </p>
  
  <p><strong>Update:</strong></p>
  
  <p>I updated the hyperlink: <code>&lt;a href="ForceDownload.cfm:filename=#Name#"&gt;#Name#&lt;/a&gt;&lt;/td&gt;</code></p>
  
  <p>and I did the following: *</p>
  
  <pre><code>   &lt;cfparam name="http://portal/regalnet_test/ForceDownload.cfm?filename=#Name#" default=""&gt;
  &lt;cfset folder = "\\oc2-web\wwwroot\RegalNet_Test\Downloads\MktgDownloads\Logos\"&gt;
  
  &lt;!--- If the path to the file is valid ---&gt;
  &lt;cfif fileExists(folder &amp; filename)&gt;
      &lt;cfset filename = name&gt;
      &lt;cfset fileInfo = GetFileInfo(folder &amp; filename)&gt;
      &lt;cfset mimeType = getPageContext().getServletContext().getMimeType(folder &amp; filename)&gt;
      &lt;cfheader name="Content-Disposition" value="attachment; filename=""#filename#"""&gt;
      &lt;cfheader name="Expires" value="#Now()#"&gt;
      &lt;cfheader name="Content-Length" value="#fileInfo.size#"&gt;
      &lt;cfcontent type="#mimeType#" file="#folder##filename#" deletefile="No"&gt;
  
  &lt;!--- Otherwise, display debugging information (Dev only) ---&gt;
  &lt;cfelse&gt;
     &lt;cfoutput&gt;
      Sorry, unable to find file: #folder##filename#&lt;br&gt;
        Does the folder exist? #directoryExists(folder)#&lt;br&gt;
      Does the File Exist? = #fileExists(folder &amp; filename)#
     &lt;/cfoutput&gt;
  &lt;/cfif&gt;
  </code></pre>