Downloading a file using cfhttp adds a new line (even to binary files)

Refresh . I found a workaround. If I send a dummy form field along with the file, it will work. Is this a ColdFusion bug, or is there something in the HTTP specification that says forms should contain at least one field without a file form?

Update 2 . I am convinced that this is cfhttp ColdFusion error. This is based on Lee's answer and on the fact that I used the code below to submit a form with only a file using javascript, and it works fine:

<form enctype="multipart/form-data" action="<cfoutput>#CGI.PATH_INFO#</cfoutput>" method="POST" name="theForm"> <input name="theFile" type="file" /><br/> </form> <a href="#" onclick="document.theForm.submit()">submit</a> 

I have a problem downloading files from a ColdFusion server to another web server. It seems that cfhttpparam type="file" indiscriminately adds a new line (carriage return and line feed) to the end of the file. It splits binary files. This does not happen when I manually upload a file through a form field. I tried with the mimetype parameter and without it, and I tried lying about mimetype with various binary formats (exe, zip, jpg), but nothing worked. Is there any parameter that I am missing, or is this a bug in ColdFusion? (I am running CF 8.0.1.195765 on WinXP.)

Below is the test code I use, it just uploads the file to the same directory. Manual download works, but server loading ends with adding CRLF to the file.

 <cfset MyDir = "C:\test" /> <cfset MyFile = "test.zip" /> <cfif IsDefined("Form.TheFile")> <cffile action="upload" fileField="theFile" destination="#MyDir#" nameConflict="MakeUnique" /> <cfelse> <cfhttp url="http://#CGI.SERVER_NAME##CGI.SCRIPT_NAME#" method="POST" throwOnError="Yes"> <cfhttpparam type="file" name="theFile" file="#MyDir#\#MyFile#" /> </cfhttp> </cfif> <html><body> <h2>Manual upload</h2> <form enctype="multipart/form-data" action="<cfoutput>#CGI.PATH_INFO#</cfoutput>" method="POST"> <input name="theFile" type="file" /><br/> <input type="submit" value="Submit" /> </form> </body></html> 
+6
coldfusion forms file-upload
source share
3 answers

or is there something in the HTTP specification that forms should contain at least one field without a file form?

I do not know for sure. But according to these definitions , it looks like a POST containing only a file should be valid. Therefore, I suspect that the problem may be CFHTTP in ACF.

According to Fiddler, the source content from the cfhttp call to ACF contains an additional new line immediately before the ending border (0D 0A in hexadecimal notation). But under Railo this is not so. Therefore, I think ACF cfhttp might be the culprit.

Code example:

 <cfhttp url="http://127.0.0.1:8888/cfusion/receive.cfm" method="post"> <cfhttpparam name="myFile" type="file" file="c:/test/testFile.zip" mimetype="application/octet-stream" /> </cfhttp> 

Railo 3.1.2 Results

 POST /railo/receive.cfm HTTP/1.1 User-Agent: Railo (CFML Engine) Host: 127.0.0.1:8888 Content-Length: 382 Content-Type: multipart/form-data; boundary=m_l7PD5xIydR_hQpo8fDxL0Hb7vu_F8DSzwn --m_l7PD5xIydR_hQpo8fDxL0Hb7vu_F8DSzwn Content-Disposition: form-data; name="myFile"; filename="testFile.zip" Content-Type: application/octet-stream; charset=ISO-8859-1 Content-Transfer-Encoding: binary PK & 1= cN'testFile.txtTestingPK & 1= cN' testFile.txtPK:1 --m_l7PD5xIydR_hQpo8fDxL0Hb7vu_F8DSzwn-- 

ACF Results (Versions 8 and 9)

 POST /cfusion/receive.cfm HTTP/1.1 Host: 127.0.0.1:8888 ... other headers removed for brevity .... Content-type: multipart/form-data; boundary=-----------------------------7d0d117230764 Content-length: 350 -------------------------------7d0d117230764 Content-Disposition: form-data; name="JobFile"; filename="c:\test\testFile.zip" Content-Type: application/octet-stream PK & 1= cN'testFile.txtTestingPK & 1= cN' testFile.txtPK:1 -------------------------------7d0d117230764-- 
+4
source

Perhaps Railo 3.1.2 and ColdFusion 9 handle this a little differently, but your code looks a little wrong for me.

Your CGI.PATH_INFO is not applicable here.

While the browser is smart enough to use a path without a hostname, CFHTTP feels better with the fully qualified hostname + script path + script. Note: cgi.SCRIPT_NAME worked in CF9, Railo required cgi.SERVER_NAME to add, although I think this is more correct in general.

This is why a slightly modified version of the code works fine for me. The zip file is downloaded and sent without damage.

the form:

 <form enctype="multipart/form-data" action="<cfoutput>#cgi.SCRIPT_NAME#</cfoutput>" method="POST"> <input name="theFile" type="file" /><br/> <input type="submit" value="Submit" /> </form> 

CFHTTP:

  <cfhttp url="#cgi.SERVER_NAME##cgi.SCRIPT_NAME#" method="POST" throwOnError="Yes"> <cfhttpparam type="file" name="theFile" file="#MyDir#/#MyFile#" /> </cfhttp> 

Hope this helps.

+1
source

I get an extra stream of lines and adding a caret to the file. The problem for me is the combination of cfhttp and cfloop. As soon as I broke the creation of the file into 3 parts: Create, cfloop endrow-1, then adding the last entry.

It seems like how to do this, but there is no extra line.

+1
source

All Articles