Reputation: 863
I have been allowing user to upload file via a form online like this:
<form action="upload.htm" enctype="multipart/form-data" name="upload_form" method="post">
<input type="file" name="upfile">
<input type="submit" name="upload" value="Upload Photos">
</form>
Then on the back end the cffile would upload the file:
<cffile action="upload" destination="#currentpath#" accept="image/jpeg, image/gif, image/png" fileField="form.upfile" nameconflict="makeunique">
Now I want to do automation so that the image file does not have to be sitting in the user's computer but rather from a web destination e.g. http://www.sample.com/images/myimage.jpg instead of c:/images/myimage.jpg
I have tried this:
<cfhttp method="get" url="http://www.example.com/images/myimage.jpg" resolveurl="Yes" throwOnError="Yes">
<cfif cfhttp.mimeType eq "image/jpeg">
<cfset currentpath = expandpath('/test/')>
<cffile action="upload" destination="#currentpath#" accept="image/jpeg, image/gif, image/png" fileField="#cfhttp.fileContent#" nameconflict="makeunique">
</cfif>
However, it is giving me an error:
Invalid content type: ''. The files upload action requires forms to use enctype="multipart/form-data"
I am not using a form to upload but it seems to require a form field.
So I tried this:
<cfhttp method="get" url="http://www.example.com/images/myimage.jpg" resolveurl="Yes" throwOnError="Yes">
<cfif cfhttp.mimeType eq "image/jpeg">
<cfset currentpath = expandpath('/test/')>
<cffile action="write" output="#cfhttp.fileContent#" file="#currentpath#/someimage.jpg">
</cfif>
This one writes out a "file" called someimage.jpg but the output is NOT a jpg file, but something unrecognizable. With the cffile "write", it doesn't allow to check for image type or same file name.
Any help is appreciated. Thank you in advance.
Upvotes: 4
Views: 1627
Reputation: 28873
Assuming the call was successful, the current code may be retrieving and/or saving the response as text, instead of binary, which would corrupt the image. To ensure you get back a binary image, use getAsBinary="yes"
.
Having said that, it is simpler to save the response to a file directly within the cfhttp call:
<cfhttp url="http://www.example.com/images/myimage.jpg"
method="get"
getAsBinary="yes"
path="#currentpath#"
file="someimage.jpg"
resolveurl="Yes"
throwOnError="Yes" />
Upvotes: 5