4

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.

Leigh
  • 28,765
  • 10
  • 55
  • 103
Jack
  • 853
  • 1
  • 7
  • 20
  • *I am not using a form to upload but it seems to require a form field.* Yes, an "upload" would require a form field, but .. that is not what the code is doing. An upload transfers a file from a client machine to the CF server. The cfhttp call is server-to-server. In this case it downloads a file from a remote onto the CF Server. – Leigh Apr 28 '17 at 02:35

1 Answers1

5

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" />
Leigh
  • 28,765
  • 10
  • 55
  • 103
  • Thank you! This works in retrieving the file. But how can I be sure that the user input URL contains an image file? I also need to ensure the output file name is the same as input file name. Any way to get the file name from cfhttp? – Jack Apr 28 '17 at 03:01
  • 3
    That would be a different question. This one has been answered. – Dan Bracuk Apr 28 '17 at 13:00
  • @DanBracuk The question was for retrieving image file in particular from the web. That's why in the original code there is a check for image file, then a file write. If you know the answer I would appreciated if you can let me know. – Jack Apr 28 '17 at 18:26
  • @Jack - Yes, but the main question was why is not the image saving correctly. The answer is, ensure image is saved as binary not text. The question of how to verify a URL exists and validate a specific content type is a separate topic. Having said that, did you try adding `getAsBinary="yes"` to your code? That may be all you need. If not, you should open a separate thread about how to validate the url and type. That will make the thread easier to find for the next guy that runs into that issue. – Leigh Apr 28 '17 at 19:44
  • @Leigh Just setting getAsBinary="yes" does not ensure only image is downloaded. I can load a pdf with it also. I think I will have to do a isimagefile check after loading it on server. – Jack Apr 29 '17 at 01:55
  • *does not ensure only image is downloaded* It is not supposed to. I think you misunderstood the latter suggestion. I meant "just add it to your *original* code". – Leigh Apr 29 '17 at 02:34