Copy link to clipboard
Copied
Doesn't seem to be much info out there on this. I'm consistently getting a coldFusion error on upload. (sorry, wasn't sure whether this was a coldFusion or Flash problem, but ended up posting here)
I have tried several ways of uploading, the latest would be:
var request:URLRequest = new URLRequest("http://www.blabla.com/upload.cfm");
request.method = URLRequestMethod.POST;
request.contentType="application/octet-stream";
var loader:URLLoader=new URLLoader();
var variables:URLVariables = new URLVariables();
//(jpgStream = a JPEGEncoder encoded bitmapData variable I prepared earlier)
variables.filedata = jpgStream;
loader.dataFormat=URLLoaderDataFormat.BINARY;
request.data = variables;
loader.load(request);
And the coldFusion upload.cfm page I'm using is:
<cftry>
<cffile action="upload"
filefield="filedata"
destination="#ExpandPath('test.jpg' )#"
nameconflict="overwrite"
accept="application/octet-stream" />
<cfcatch type="any">
<cfdocument format="PDF" overwrite="yes" filename="errordebug.pdf">
<cfdump var="#cfcatch#"/>
</cfdocument>
</cfcatch>
</cftry>
Something worth pointing out before anyone looks too deeply for problems in the cfm page is that this works fine, when I use FileReference.upload() to upload a local file.
In the errordebug.pdf ColdFusion returns:
CType application/octet-stream
Detail CFFILE action="upload" requires forms to use enctype="multipart/form-data".
Message Invalid content type: "application/octet-stream".'
Any ideas/tips?
Copy link to clipboard
Copied
I've done this before and think I posted it a couple of years back, but it looks like all the code snippets are gone from those old posts.
Here's the upload.cfm script I used (I think)
<cfsetting enablecfoutputonly="yes">
<cfset x = GetHttpRequestData()>
<cfif IsBinary(x.content)>
<cfscript>
FileWrite("C:\wamp\www\cfmtest\test.jpg", x.content);
</cfscript>
<cfoutput>result=1</cfoutput>
<cfelse>
<cfoutput>result=0</cfoutput>
</cfif>
And you need to send the jpeg data as raw binary with the request, not as URLVariables. It's not actually a 'file' upload but just raw jpeg file data.
Something like this:
var upLoader:URLLoader=new URLLoader();
//cfm script returns the result=0 or 1 response
upLoader.dataFormat= URLLoaderDataFormat.VARIABLES;
upLoader.addEventListener(Event.COMPLETE, upLoaded);
//byteArr in here is the jpeg encoded bytearray from the image, using the JPEGEncoder class//I guess this would be your jpegStream....
function doUpload(byteArr:ByteArray):void{
//the url of the upload.cfm script
var upload_request:URLRequest = new URLRequest("http://localhost/cfmtest/upload.cfm");
upload_request.data = byteArr;
upload_request.method= URLRequestMethod.POST;
upload_request.contentType="application/octet-stream";
upLoader.load(upload_request);
}
//handle the response from the server.
function upLoaded(event:Event):void {
trace((upLoader.data.result=="1")?"your image was saved":"the server did not recognize the image data");
}
I've copied and pasted snippets, I don't have coldfusion installed on this machine, so can't test it, but I'm pretty sure that is close to what you need. You should be able to tweak the cfm to get it working like you want....
Copy link to clipboard
Copied
Brilliant, thanks for that Greg. Even flying blind, you've come through again!
Had to tweak a little, as I believe 'FileWrite' arrived with ColdFusion 8, and the company i'm working for hasn't yet upgraded from 7. Had to use the cffile tag instead. Also I kept my outputting PDFs rather than returning errors. May play around with this in future but the main thing is, i'm seeing my jpeg upload!
For anyone interested, my working code at the moment is:
ActionScript:
var loader:URLLoader=new URLLoader();
loader.dataFormat= URLLoaderDataFormat.TEXT;
configureListeners(loader);
var request:URLRequest = new URLRequest("http://www.blabla.com/upload.cfm
?filename=test.jpg");//(jpgStream = a JPEGEncoder encoded bitmapData variable I prepared earlier)
request.data = jpgStream;
request.method= URLRequestMethod.POST;
request.contentType="application/octet-stream";
loader.load(request);
ColdFusion upload.cfm
<cftry>
<cfset x = GetHttpRequestData()>
<cfif IsBinary(x.content)>
<cflock name="MyBinaryLock" type="Exclusive" timeout="30">
<cffile action="write" file="#ExpandPath(URL.filename)#" output=#x.content#/>
</cflock>
</cfif>
<cfcatch type="any">
<cfdocument format="PDF" overwrite="yes" filename="errordebug.pdf">
<cfdump var="#cfcatch#"/>
</cfdocument>
</cfcatch>
</cftry>
Copy link to clipboard
Copied
Awesome Craig! CF really isn't my thing, I remember spending more time than I'd have liked in the docs for that small snippet lol.
I remember trying to get it to work via remoting as well which I did at the time, albeit in a less than preferred kind of way. I couldn't figure out how to switch on the amf3 support mode in CF (client side in AS is easy) for regular flash remoting calls (Flex RemoteObjects where set up for amf3, but I couldn't figure out how to switch that on in the CF admin settings for plain actionscript calls). I ended up doing it by base64 encoding the image before sending it via an amf0 remoting call.