How to get CFHTTP with a file parameter to show only the file name and not the full path?

We are trying to interact with a RESTful web service that is expecting a file.

I set the field name for the data (as required by the API) and then point the file as an absolute path. When a file is sent to the server, the file name in the HTTP transaction is the complete absolute path.

This causes an API problem since the full path is then written as "FileName".

How do I get ColdFusion to report only the file name and not the full path?

We are using ColdFusion 9.

Here is CFML:

<cfhttp url="http://server/testcode" port="9876" method="post" result="Content"> <cfhttpparam type="file" name="data" file="c:\temp\testfile.txt"> </cfhttp> 

Here are some examples of how HTTP interacts with various browsers:

 CFHTTP 9 -------------------------------7d0d117230764 Content-Disposition: form-data; name="data"; filename="c:\temp\testfile.txt" Content-Type: text/plain This is the text, really long, well, not really. -------------------------------7d0d117230764-- IE8 -----------------------------7db370d80e0a Content-Disposition: form-data; name="FileField"; filename="C:\temp\testfile.txt" Content-Type: text/plain This is the text, really long, well, not really. -----------------------------7db370d80e0a-- Chrome 13 ------WebKitFormBoundaryDnpFVJwCsZkzTGDc Content-Disposition: form-data; name="FileField"; filename="testfile.txt" Content-Type: text/plain This is the text, really long, well, not really. Firefox 6 -----------------------------22798303036224 Content-Disposition: form-data; name="FileField"; filename="testfile.txt" Content-Type: text/plain This is the text, really long, well, not really. -----------------------------22798303036224-- 

Obviously, IE8 and CFHTTP do the same (add "c: \ temp" to the file name). I'm not sure what the spec is for HTTP, but it would be nice if there was a way to get CFHTTP to leave the path.

Is there any way to do this?

+4
source share
4 answers

I ran into a problem like yours once. I did not care about the path exception, but I wanted to send a different file name than the file name in my server file system. I couldn't find a way to do this with CF tags at all, but I was able to get it working by switching to Java. I used org.apache.commons.httpclient which comes with CF9 IIRC. This is something like this (sorry for any typos, I am transferring from more complex code):

 oach = 'org.apache.commons.httpclient'; oachmm = '#oach#.methods.multipart'; method = createObject('java', '#oach#.methods.PostMethod').init(post_uri); filePart = createObject('java', '#oachmm#.FilePart').init( 'fieldname', 'filename', createObject('java', 'java.io.File').init('filepath') ); method.setRequestEntity( createObject('java', '#oachmm#.MultipartRequestEntity').init( [ filePart ], method.getParams() ) ); status = createObject('java', '#oach#.HttpClient').init().executeMethod(method); method.releaseConnection(); 
+5
source

I see that the content type is text / plain, so first I think you need to add the multipart property to CFHTTP

 <cfhttp url="http://server/testcode" port="9876" method="post" result="Content" multipart = "yes"> <cfhttpparam type="file" name="data" file="c:\temp\testfile.txt"> </cfhttp> 

May solve your problem.

+3
source

The only difference that I see between all the messages is that the CF sends name="data" and the rest sends name="FileField" . If the other browser views are correct, I would cfhttpparam your cfhttpparam :

 <cfhttpparam type="file" name="FileField" file="c:\temp\testfile.txt"> 

or even try sending an additional parameter to FileName:

 <cfhttpparam type="file" name="data" file="c:\temp\testfile.txt" /> <cfhttpparam type="formField" name="FileName" value="testfile.txt" /> 
+2
source

So I was able to access the API and make it work. Here is the code for this specific part (since I assume you were able to log in and get the guid document).

 <!--- upload a document ---> <cfhttp method="post" url="<path to watchdox api upload>/#local.guid#/upload"> <cfhttpparam type="header" name="Content-type" value="multipart/form-data"> <cfhttpparam type="header" name="x-wdox-version" value="1.0"> <cfhttpparam type="header" name="x-wdox-ssid" value="#local.xwdoxssid#" > <cfhttpparam type="formfield" name="filename" value="testfile.txt" > <cfhttpparam type="file" file="c:\temp\testfile.txt" name="data" > </cfhttp> 

Hope this helps.

0
source

All Articles