Coldfusion writing binary file to output stream
January 29th, 2007
I recently had a need to grab a binary file from an http response and write that back to the output stream instead of writing the file to the disk. The reason was because we were using the flash client to play the file and it was requesting the file from a cfc call.
Ordinarily you would probably want to use something like cfcontent but, in this situation we were going for speed and there was also an issue with getting the file duration (we are sending back mp3's) in the flash client with cfcontent.
So here is the method you need to use:
<cfif isBinary(theFile)>
<cfset fomattedName = "your-file-name"/>
<cfheader name="Content-Disposition" value="inline; filename=#fomattedName#.mp3">
<cfscript>
context = getPageContext();
context.setFlushOutput(false);
response = context.getResponse().getResponse();
out = response.getOutputStream();
response.reset();
response.setContentType("audio/mpeg");
response.setContentLength(arrayLen(theFile));
out.write(theFile);
out.flush();
out.close();
</cfscript>
</cfif>
Sorry, comments are closed for this article.