Posting a file to a form with CFHTTP

I'm trying to post an XML file to a multipart HTML form, it's not working, and I can't figure out what the problem is. It appears that ColdFusion is just not transmitting the file. I've tried posting to the actual form I need to post to as well as a test page that dumps requests, and all that comes through are the form fields.

Here's the relevant part of my code:

  <cfhttp url="#endPoint#" method="post" multipart="yes">
        <cfhttpparam type="formField" name="file_name" value="test.xml">
        <cfhttpparam type="formField" name="user_name" value="test">
        <cfhttpparam type="formField" name="password" value="test">
        <cfhttpparam type="file" name="test.xml" file="#localfile#">
    </cfhttp>

I can confirm that endPoint points to a valid URL, the formField names/values are valid, and that test.xml does indeed exist at the location specified by localfile.

Suggestions? Any input would be appreciated!

Add'l info: I am on CF MX 6.1 if that makes a difference.

EDIT: After reviewing the input below, I did some more testing.

I can confirm that localfile is an absolute path (C:path_to_my_filestest.xml).

I'm not sure what the target form is running on. I don't know for sure, but I don't think it's ColdFusion.

I built my own HTTP test page using GetHTTPRequestData() as mentioned below, and I think that works ok, though it looks a litle strange to me. Writing GetHTTPRequestData().content to a file lets me see what I'm trying to send. However, the final form still reports that I'm not including a file. Posttestserver.com also reports no file. I've included the result from both my page and the POST test server below.

My test page result:

-------------------------------7d0d117230764
Content-Disposition: form-data; name="file_name"
Content-Type: text/plain; charset=UTF-8

test.xml
-------------------------------7d0d117230764
Content-Disposition: form-data; name="user_name"
Content-Type: text/plain; charset=UTF-8

test
-------------------------------7d0d117230764
Content-Disposition: form-data; name="password"
Content-Type: text/plain; charset=UTF-8

test
-------------------------------7d0d117230764
Content-Disposition: form-data; name="test.xml"; filename="C:my_filestest.xml"
Content-Type: text/xml

%3C%3Fxml%20version%3D%221%2E0%22%20encoding%3D%22UTF%2D8%22%20%3F%3E%3CjobFeed%3E%3Cjob%3E%3CjobId%3E1234%3C%2FjobId%3E%3CjobTitle%3ETest%20Job%3C%2FjobTitle%3E%3CjobCity%3ETest%20City%3C%2FjobCity%3E%3CjobState%3ETest%20State%3C%2FjobState%3E%3CjobDescription%3ETest%20Description%3C%2FjobDescription%3E%3CjobZip%3E12345%3C%2FjobZip%3E%3CjobUrl%3Ehttp%3A%2F%2Fwww%2Etest%2Ecom%3C%2FjobUrl%3E%3CJobType%3ETEC%3C%2FJobType%3E%3C%2Fjob%3E%3C%2FjobFeed%3E


-------------------------------7d0d117230764--

I've got no idea what this "-------------------------------7d0d117230764" business is.

Here's what I got from the test site:

Headers (Some may be inserted by server)
UNIQUE_ID = TlZTra3sqvkAAECsSBsAAAAL
HTTP_HOST = www.posttestserver.com
HTTP_CONNECTION = close
HTTP_USER_AGENT = ColdFusion
HTTP_ACCEPT_ENCODING = deflate, gzip, x-gzip, compress, x-compress
CONTENT_TYPE = multipart/form-data; boundary=-----------------------------7d0d117230764
CONTENT_LENGTH = 1159
GATEWAY_INTERFACE = CGI/1.1
REQUEST_METHOD = POST
QUERY_STRING = 
REQUEST_URI = /post.php
REQUEST_TIME = 1314280365

Post Params:
key: 'file_name' value: 'test.xml'
key: 'user_name' value: 'test'
key: 'password' value: 'test'

== Begin post body ==

== End post body ==

Here it appears I've transmitted no file.

I'm still looking at it, but I'm not seeing the problem. Ideas?


If the endPoint is a CF page, then I am wondering if something else may be going on. The data is sent in the request body, yes. But with CF pages, it should still parse the information and create a form field for that file. Same as with a regular form upload. In this case the field name would be form["test.xml"] . Could that be part of the issue?

CFDUMP results under MX6.1 and CF9

FIELDNAMES FILE_NAME,USER_NAME,PASSWORD,TEXT.XML 
FILE_NAME test.xml 
PASSWORD test 
TEXT.XML C:CFusionMX...tempwwwroot-tmpneotmp6275345679234991.tmp 
USER_NAME test 

Note: One difference under CF9 was that getHttpRequestData().content is empty. Whereas under MX6 it is still populated. Seems like 6.1 preserves a copy of the data (after processing it) but CF9 does not. Not sure why.


When you specify a type of file, then the file data is sent in the post body, not in a form field name that you can reference. If you check the LiveDocs on CFHTTPPARAM it states for the type="file" attribute:

The absolute path to the file that is sent in the request body.

So as Leigh stated, on your receiving page, you need to use GetHttpRequestData , something like this:

<cfset objRequest = GetHttpRequestData() />
<cfset object = objRequest.Content() />
链接地址: http://www.djcxy.com/p/31246.html

上一篇: ColdFusion文件上传问题

下一篇: 使用CFHTTP将文件发布到表单