DEV Community

James Moberg
James Moberg

Posted on

3 1

Using Try/Catch to Suppress Aborted Download Errors

After upgrading some projects to ColdFusion 2016, we started encountering errors in the logs that were related to client-aborted downloads.

java.io.IOException: An established connection was aborted by the software in your host machine.

After determining that it wasn't a server problem (ColdFusion, IIS, WAF, etc), we added some try/catch logic to suppress the error from showing up in our logs.

Here's some sample CFML code that we've been using. (DISCLAIMER: We don't use or recommend CFContent on projects that use very large files, take a long time to generate or require streaming.)

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

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs