Use Content Length When Streaming Binary Variables In ColdFusion

Posted July 25, 2007 at 6:27 PM by Ben Nadel

Tags: ColdFusion

I was just reading over on cf.Objective() about how it's important to use Content-Length when streaming files to the client. Without it, the browser knows how much it has downloaded so far, but not necessarily how much it will have to download in total (I guess it goes till some sort of End Of File marker?). I can understand the need for this fix - there is perhaps nothing more frustrating than having no idea how much file you have left to download.

The problem is that while I sometimes stream actual files, it is more common that I stream binary data using ColdFusion CFContent's Variable attribute. Jared only covered the File attribute, so I wasn't sure what to do in this case. But after a bit of testing, it looks like the browser is happy know the number of bytes in the byte array that represents the binary object:

  • <!--- Kill extra output. --->
  • <cfsilent>
  •  
  • <!--- Read in the image binary. --->
  • <cffile
  • action="readbinary"
  • file="#ExpandPath( './test_girl.jpg' )#"
  • variable="binImage"
  • />
  •  
  •  
  • <!---
  • Set the content disposition and give a suggested
  • file name for the client.
  • --->
  • <cfheader
  • name="content-disposition"
  • value="attachment; filename=test_girl.jpg"
  • />
  •  
  • <!---
  • Set the content length. This is the length of the
  • byte array that represenets the binary image object.
  • --->
  • <cfheader
  • name="content-length"
  • value="#ArrayLen( binImage )#"
  • />
  •  
  • <!---
  • Set the content type and then stream the binary
  • image to the client.
  • --->
  • <cfcontent
  • type="image/jpeg"
  • variable="#binImage#"
  • />
  •  
  • </cfsilent>

This downloads the file nicely, and when I check the headers in FireBug, the content length is set correctly. Thanks Jared, good tip.

Also, as a funny little thing, if I increase the content-length by doing something like this:

  • #(ArrayLen( binImage ) + 1)#

... FireFox will actually never stop trying to download the image! I guess it just won't give up hope on that last byte :) So, I guess the moral of the story is that if you are going to provide a content length to the browser, make sure that it is correct otherwise you will do more damage than good.




Reader Comments

Jul 25, 2007 at 8:27 PM // reply »
32 Comments

Cool tip! I'll definitely remember this when I start working on an app in a few weeks that will be uploading/downloading files.

CooLJJ


Jul 25, 2007 at 9:25 PM // reply »
1 Comments

Cool. I will be using that at work tomorrow.


Aug 1, 2007 at 5:48 PM // reply »
18 Comments

Ben,

This is good, but not perfect. Docs say:

"Note: This action reads the file into a variable in the local Variables scope. It is not intended for use with large files, such as logs, because they can bring down the server."

What if you allow users/customers to download files routinely in the megs.

I haven't found a good solution to be able to give the file size for large files. Any ideas?


Aug 2, 2007 at 7:43 AM // reply »
11,246 Comments

@Sami,

To be honest, I don't deal with a lot of file security most of the time. Therefore, I usually just link to files directly on the public server which bypasses all the ColdFusion stuff and just lets IIS deal with the file transfer.

But I agree with the documentation - this is not a good idea for large files. If you need file security, I guess the best thing to do is copy the file to a temp directory and then forward the person to it.


Aug 2, 2007 at 6:38 PM // reply »
18 Comments

This would be a good test:

Compare CF's native approach to using the java.io class, across multiple file sizes.

http://www.exampledepot.com/egs/java.io/GetLength.html

Sami


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 25, 2013 at 10:08 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Ben, my question is that i want the current node with its tag and its parent node. i just want only that data. So, give me the solution for that. and remember solution is working on " xpath 1.0 ... read »
May 25, 2013 at 10:01 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
hey ben, i want get my current node tag and also want the root node tag withing. So, how can i fix it.. ! ... read »
May 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools