tags:

views:

48

answers:

4

Does anyone have an example of how Coldfusion's GetHttpRequestData() works? I'm looking to use this func to save data from the AJAX Upload script: http://valums.com/ajax-upload/

The script works in FireFox but not Safari, Chrome, etc...

Ideas?

A: 

You want to look into using cffile with action="upload" to upload the file: http://cfdocs.org/cffile

Pete Freitag
A: 

GetHttpRequestData() is intended for decoding protocols like SOAP, XML-RPC, and some of the more complex REST-ful protocols. HTTP file uploads are normally done as POSTs using the multipart/form-data MIME type. Looking at http://www.cfquickdocs.com/it doesn't appear that GetHttpRequestData() has any special support for multipart data, which means you'd have to split and decode the parts yourself. Not my idea of fun, and completely unnecessary if you're just doing file uploading.

<cffile action="upload"> or <cffile action="uploadAll"> (new for CF9) should be quite sufficient for processing file uploads, even for those done via an AJAX upload script.

Boomerang Fish
A: 

You might also want to read this recent thread about that script. As valums suggested, you should be able to extract the binary data from getHttpRequestData().content (when needed).

In my very limited tests, it seemed to work okay with IE8/FF/Chrome/Opera. However, I had no luck with Safari (windows). It seemed like the request data was getting mangled (or possibly misinterpreted by CF?). So the final content-type header reported by CF was incorrect, causing an http 500 error. Granted, I did not test this extensively.

Here is my quick and dirty test script (lame by design...)

<cfset uploadError = "" />
<cfif structKeyExists(FORM, "qqFile")>
    <!--- upload as normal --->
    <cffile action="upload" filefield="qqFile" destination="c:/temp" />
<cfelseif structKeyExists(URL, "qqFile")>
    <!--- save raw content. DON'T do this on a prod server! --->
    <!--- add security checks, etc... --->
    <cfset FileWrite( "c:/temp/"& url.qqFile, getHttpRequestData().content) />
<cfelse>
    <!--- something is missing ...--->
    <cfset uploadError = "no file detected" />
</cfif>

<!--- return status old fashioned way (for compatibility) --->
<cfif not len(uploadError)>
    {"success": true}
<cfelse>
    <cfoutput>{error": "#uploadError#"}</cfoutput>  
</cfif>
Leigh