Using CFHeader With File Names Containing Spaces (Thanks Elliott Sprehn!)

Posted July 18, 2007 at 6:28 PM

Tags: ColdFusion

Just a minor note that I thought I would post up here (since I didn't know this before and I had to test it for myself). When it comes to web development, pretty much all of my file names are purely alpha-numeric with underscores for spaces. As such, I have never come up against a problem in ColdFusion where I needed to used space-containing file names in conjunction with the CFHeader content-disposition value. In fact, I've never even thought about it.

In my post on using additional path information to alter the browser's Save-As behavior, Elliott Sprehn pointed out to me that in order to handle spaces in file names, you have to quote the file names. And, not only do you have to quote it, you have to use double quotes, NOT single quotes - single quotes do nothing to help you out and the first single quote actually shows up in the file name (before the name gets truncated).

Here is my test ColdFusion template. All it does is try to server up a file as an attachment:

 Launch code in new window » Download code as text file »

  • <!--- Kill extra output. --->
  • <cfsilent>
  •  
  • <cfheader
  • name="content-disposition"
  • value="attachment; filename=""This Is A Test.txt"""
  • />
  •  
  • <cfcontent
  • type="text/plain"
  • variable="#ToBinary( ToBase64( 'Test Data' ))#"
  • />
  •  
  • </cfsilent>

Notice that in order to get the quotes to work, you have to double them up (otherwise ColdFusion will think you are ending the string). You could also use single quotes for the value attribute, but that's not my style.

Running the above template directly, I am correctly prompted by FireFox to download the file with the proper name:


 
 
 

 
Streaming Files With Spaces Using CFHeader / CFContent  
 
 
 

I probably won't switch over to using file names with spaces, but in something like a document management system, I can see where knowing this would be super helpful.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jul 19, 2007 at 1:28 AM // reply »
18 Comments

I think you have triple quotes at the end there...


Jul 19, 2007 at 5:10 AM // reply »
18 Comments

Sami, the third quote is closing the one started at:
value="attachment

Another reason not to use spaces in filenames. Would it work if you substituted them with %20 ?


Jul 19, 2007 at 7:38 AM // reply »
6,516 Comments

@Duncan,

I just tried it and the %20 actually shows up in the file name.


Jul 20, 2007 at 4:48 AM // reply »
15 Comments

Is this actually ColdFusion doing this, or the browser?


Jul 20, 2007 at 6:57 AM // reply »
6,516 Comments

@Peter,

When I look at the http headers that get sent back with the file request (Thanks god for FireBug!!), the double quotes are coming back in the actual header value:

filename="your file name"

Therefore, I assume it is the browser that requires them. If it was ColdFusion handling it, I am sure the use of double OR single quotes wouldn't matter.


Jul 31, 2007 at 1:34 PM // reply »
125 Comments

You're welcome Ben!

@Peter

It's the browser. http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download


JC
Nov 5, 2007 at 3:26 PM // reply »
1 Comments

Did you know that IE7 puts underscores in the name of the file when you use the double quote method? I have an Intranet Document Management System that has been working great until we upgraded to IE7. If you take out the double quotes, it works perfectly for IE7.

Any ideas other than recognizing the browser and placing separate code for each? I'm thinking I have no choice at this point.

JC


Nov 5, 2007 at 3:59 PM // reply »
6,516 Comments

@JC,

Sorry, no ideas. I haven't even upgraded to IE7 yet, so I wouldn't even know how to test.


Jun 9, 2009 at 2:03 AM // reply »
1 Comments

Thanks for this, never thought of putting double quotes - was putting single quotes and of course they didn't work.

Another few hours saved because of your excellent blog - thanks!


Jun 9, 2009 at 8:12 AM // reply »
6,516 Comments

@Felix,

Glad to help out :)


Mac
Oct 1, 2009 at 11:02 AM // reply »
1 Comments

Thanks! Always had this problem and I was stripping the spaces to the consternation of the users who wanted to preserve the file name.


Oct 23, 2009 at 2:24 AM // reply »
1 Comments

Thanks a ton Ben!!!

Google + Ben Nadel save the day again!


Oct 31, 2009 at 3:39 PM // reply »
6,516 Comments

@Mac, @Will,

Glad to help out fellas.


Nov 12, 2009 at 5:39 PM // reply »
1 Comments

I actually did it this way;

<cfheader name="Content-Disposition" value="attachment;filename=#chr(34)##getimage.originalfile##chr(34)#">


Nov 15, 2009 at 7:49 PM // reply »
6,516 Comments

@Mark,

Nice, that should work. By using the chr() method, you don't have to escape the quotes by doubling them up. Good thinking.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »