Skip to content

Instantly share code, notes, and snippets.

@JamoCA
Last active January 3, 2020 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamoCA/52c17718ab6a4e047994c6bf3f7dd1f4 to your computer and use it in GitHub Desktop.
Save JamoCA/52c17718ab6a4e047994c6bf3f7dd1f4 to your computer and use it in GitHub Desktop.
Since upgrading to ColdFusion 2016, downloads that are aborted or timed-out now throw a CF error. Here's some sample code on how to prevent it using try/catch.
<!--- 20200103
Since upgrading some applications to ColdFusion 2016, downloads that are aborted or timed-out now throw a CF error.
Here's some sample code on how to prevent it using try/catch.
https://gist.github.com/JamoCA/52c17718ab6a4e047994c6bf3f7dd1f4
https://dev.to/gamesover/using-try-catch-to-suppress-aborted-download-errors-3j42
--->
<cfset FileData = {
"name" = "Filename with Spaces.pdf",
"path" = "c:\files\File12345.pdf"
}>
<!--- increase timeout as request will take longer as user may have to specify download location --->
<cfsetting requesttimeout="1200">
<!--- Use double quotes for filename in case it contains spaces --->
<cfheader name="Content-disposition" value="attachment; filename=""#FileData.Name#""">
<cftry>
<!--- Use "application/unknown" instead of file-related mime type in order to force download --->
<!--- Use "deletefile" flag to be safe (Past bug: CFMailParam defaults to "no", but still deleted the file.) --->
<cfcontent type="application/unknown" file="#FileData.Path#" deletefile="No">
<cfcatch>
<!--- log failure; optional; it's good to have this data in case there's a pattern of failure --->
<cflog file="aborteddownloads" type="information" application="yes" text="#CGI.Remote_Addr#|#FileData.Path#|#CGI.HTTP_User_Agent#|#cfcatch.message#">
Download Aborted
<cfabort>
</cfcatch>
</cftry>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment