coldfusioncfhttpcffile

Get a file from auto download url in coldfusion


I am trying to get a file from a auto download url using cfhttp. I am using the following code:

<cfhttp method="get" url="http://www.example.com/getfile" path="E:/" file="abc.csv">

In this case I have specified the file type as CSV so I am able to get the file but file type can change. I tried CFHTTP.MIMETYPE to get the file type and use like this:

<cfhttp method="get" url="http://www.example.com/getfile">
<cffile action="write" file="E:/abc.#listLast(cfhttp.MIMETYPE,'/')#" output="#cfhttp.FileContent#">

And this is working for CSV and XML files. But I want it to work to Excel files also.

Please help. Thanks in advance.


Solution

  • <cfhttp method="get" url="http://www.example.com/getfile">
    <cfset fileName = listlast(cfhttp["responseHeader"]["content-disposition"],";=")>
    <cffile action="write" file="E:/abc.#fileName#" output="#cfhttp.FileContent#">