Get a file from auto download url in coldfusion
Asked Answered
M

2

7

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.

Monophagous answered 12/2, 2015 at 7:29 Comment(7)
Have you tried setting getAsBinary="Auto" in the cfhttp tag?Erelia
I can get it as binary but still how to get file extension?Monophagous
@cfqueryparam Is there any way I can get the file extension?Monophagous
Dump the cfhttp object to view the headers. Usually a file name is included in the "Content-Disposition" header.Ref
@Ref Yes the filename is there in "Content-Disposition" only for auto download urls.Monophagous
Side note, obviously your real code should have some error handling. Check the status of the cfhttp call first. Only save the file to disk if the call was successful.Ref
@Ref Yes I am doing the sameMonophagous
U
5
<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#">
Uncertainty answered 13/2, 2015 at 7:14 Comment(1)
Why do you use two listlasts? You can cover it with one listlast and specifying both delimiters together. That's one place that listlast differs from java's split, for instance. Also, why use structfind? cfhttp["responseHeader"]["Content-Disposition"] will serve the same purpose since StructFind() throws an exception if the key doesn't exist just as directly calling it does. These are semantics though, the answer is fine, but listlast(cfhttp["responseHeader"]["content-disposition"],";=") will achieve the same goal.Erelia
N
1

As "Regular Jo" mentioned, you need to add getAsBinary="Auto" to the cfhttp to get this working. Thank you to Deepak Kumar Padhy for the inital answer that pointed me in the right direction.

<cfhttp method="get" url="http://www.example.com/getfile" getAsBinary="Auto" result="cfhttp">
        
<cfdump var="#cfhttp#">
        
<!--- Returns the file name with double quotes, ex. '"Users.zip"' --->
<cfset fileNameWithQuotes = listlast(cfhttp["responseHeader"]["content-disposition"],";=")>
        
<!--- Remove the '"' in the file name so it is Users.zip --->
<cfset fileName = REPLACENOCASE(fileNameWithQuotes,'"','', 'ALL')>
       
<!--- Write the zip to the server location ---> 
<cffile action="write" file="E:/abc/#fileName#" output="#cfhttp.FileContent#">
        
<!--- if zip file, unzip the file to get the csv --->
<cfzip file="E:/abc/#fileName#" action="unzip" filter="*.csv" destination="E:/abc/" />
Northeastwards answered 30/5, 2021 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.