Download JPG’s with ColdFusion from a Linux server

Posted on July 7, 2006 | 2 comments

[lang_en]
I am working on a Flex and ColdFusion file upload/download component and came across the fun Linux and Windows file format issues. The files will upload correctly but if you try and pull down a JPG file form the linux server with ColdFusion’s tag with the default charset it will not come down correctly.

<cffile action="read"
   file="#folder#/#filename#" variable="fileData"/>
    <cfcatch>
        <cflog text="Invalid read #folder#/#filename#">
    </cfcatch>
</cftry>
<cfif fileData NEQ ""><cfcontent reset="yes"><cfoutput>#fileData#</cfoutput></cfif>

For JPG’s the code above will write the file as strange charactors and cause the JPG be unusable. You must set the “charset” attribute in the tag. Here is the code that worked for me:

<cffile action="read" charset="iso-8859-1"
   file="#folder#/#filename#" variable="fileData"/>
    <cfcatch>
        <cflog text="Invalid read #folder#/#filename#">
    </cfcatch>
</cftry>
<cfif fileData NEQ ""><cfcontent reset="yes"><cfoutput>#fileData#</cfoutput></cfif>

I assume it could even be another value depending on on the server’s default values. So just beware of the charset value issues when downloading binary files with ColdFusion from linux servers.
[/lang_en]
[lang_zh]
与Flex和Coldfusion案卷upload/下载component操作时我碰到一些好玩的Linux 和Windows file format的问题。Upload一些案卷没问题但是,除非你使用Coldfusion的缺省charset tag, Linux服务器的JPG就办不动。

<cffile action="read"
   file="#folder#/#filename#" variable="fileData"/>
    <cfcatch>
        <cflog text="Invalid read #folder#/#filename#">
    </cfcatch>
</cftry>
<cfif fileData NEQ ""><cfcontent reset="yes"><cfoutput>#fileData#</cfoutput></cfif>

用上述的代码会使JPG”不能用”。在tag内你必须设定”charset”。下面的代码有效。

<cffile action="read" charset="iso-8859-1"
   file="#folder#/#filename#" variable="fileData"/>
    <cfcatch>
        <cflog text="Invalid read #folder#/#filename#">
    </cfcatch>
</cftry>
<cfif fileData NEQ ""><cfcontent reset="yes"><cfoutput>#fileData#</cfoutput></cfif>

依靠服务器的缺省, 我认为还有别的办法。所以当你想从Linux 服务器,用ColdFusion下载二进制案卷时,要小心charset的问题。
[/lang_zh]

  • Cédric

    Hello,

    I’ve the same problem. When I upload a document (word, excel, …), the files seems to be uploaded correctly. But when I try to download it (using cffile tag), the file is corrupted (invalid charset I think).

    Can you help me . Do you have found any solution ?

    Thanks,
    Ced

  • http://www.renaun.com Renaun Erickson

    <cffile action=”read” charset=”iso-8859-1″ …

    Worked for me, here is a list of all the valid charsets try them all:

    * utf-8
    * iso-8859-1
    * windows-1252
    * us-ascii
    * shift_jis
    * iso-2022-jp
    * euc-jp
    * euc-kr
    * big5
    * euc-cn
    * utf-16

    http://livedocs.macromedia.com/coldfusion/7/htmldocs/00000250.htm