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

从coldfusion中的自动下载url获取文件,coldfusion,cfhttp,cffile,Coldfusion,Cfhttp,Cffile,我正在尝试使用cfhttp从自动下载url获取文件。 我正在使用以下代码: <cfhttp method="get" url="http://www.example.com/getfile" path="E:/" file="abc.csv"> 这适用于CSV和XML文件。但我希望它也能用于Excel文件 请帮忙。 提前感谢。 <cfhttp method="get" url="http://www.example.com/getfile"> <cfset fil

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

<cfhttp method="get" url="http://www.example.com/getfile" path="E:/" file="abc.csv">
这适用于CSV和XML文件。但我希望它也能用于Excel文件

请帮忙。 提前感谢。


<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#">
正如前面提到的“常规作业”一样,您需要将getAsBinary=“Auto”添加到cfhttp以使其正常工作。感谢迪帕克·库马尔·帕迪的最初回答,它为我指明了正确的方向

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


您是否尝试过在cfhttp标记中设置
getAsBinary=“Auto”
?我可以将其设置为二进制,但仍然可以获取文件扩展名?@cfqueryparam是否有任何方法可以获取文件扩展名?转储cfhttp对象以查看标题。通常,“内容处置”标题中包含一个文件名。@是的,“内容处置”中的文件名仅用于自动下载URL。为什么使用两个ListLast?您可以使用一个listlast并同时指定两个分隔符来覆盖它。例如,这是listlast与java的split不同的地方。还有,为什么要使用structfind
cfhttp[“responseHeader”][“Content Disposition”]
也会起到同样的作用,因为
StructFind()
会在键不存在时抛出异常,就像直接调用它一样。这些都是语义,答案很好,但是
listlast(cfhttp[“responseHeader”][“content disposition”],“;=”)将实现相同的目标。@cfqueryparam:谢谢你的建议。我已经更新了我的答案。
<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/" />