从coldfusion中获取自动下载URL的文件

时间:2015-02-12 07:29:03

标签: coldfusion cfhttp cffile

我正在尝试使用cfhttp从自动下载网址获取文件。 我使用以下代码:

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

在这种情况下,我已将文件类型指定为CSV,因此我可以获取文件但文件类型可以更改。 我尝试CFHTTP.MIMETYPE获取文件类型并使用如下:

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

这适用于CSV和XML文件。但是我希望它也适用于Excel文件。

请帮忙。 提前谢谢。

2 个答案:

答案 0 :(得分:4)

<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#">

答案 1 :(得分:0)

正如“Regular Jo”所提到的,您需要将 getAsBinary="Auto" 添加到 cfhttp 才能使其正常工作。感谢 Deepak Kumar Padhy 的初步回答,为我指明了正确的方向。

<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/" />