user747291
user747291

Reputation: 821

cfhttp to get an image and save to the disk

I am trying to get an image from a url and save it to the local using cfhttp like below.

<cfhttp
    timeout="45"
    throwonerror="false"
    url="http://domain/images/cme_recorder_on.png"
    method="get"
    useragent="Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12"
    getasbinary="yes"
    result="local.objGet"
>

<cfset myImage = ImageNew(local.objGet.FileContent)>

<cfif (FindNoCase( "200", local.objGet.Statuscode ) AND FindNoCase( "image", local.objGet.Responseheader["Content-Type"]
        ))>   
      <cfoutput>this is an image</cfoutput>
       <cffile file="#myImage#" action="write" output="c:\test_myImage.jpg"> 
<cfelse>
     <cfabort>   
</cfif>

I have tried using cffile write to save the image to the local. I do not see any image being writted into c:. Is this the right way to save the image to disk? Thanks all

Upvotes: 1

Views: 4209

Answers (2)

Edy Ionescu
Edy Ionescu

Reputation: 1773

The source for imageNew can be a URL, so there's no need for cfhttp in this case.

<cfset newImage = imageNew( "http://domain/images/cme_recorder_on.png" )>
<cfset imageWrite(newImage, "c:\test_myImage.jpg")>

Upvotes: 4

Sean Coyne
Sean Coyne

Reputation: 3884

Use imageWrite() https://wikidocs.adobe.com/wiki/display/coldfusionen/ImageWrite

So for your example, instead of using cffile you would use

<cfset imageWrite(myImage, "c:\test_myImage.jpg") />

Upvotes: 2

Related Questions