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

Posted July 18, 2007 at 6:28 PM by Ben Nadel

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:

  • <!--- 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.




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 »
11,314 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 »
11,314 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 »
132 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 »
11,314 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 »
11,314 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 »
3 Comments

Thanks a ton Ben!!!

Google + Ben Nadel save the day again!


Oct 31, 2009 at 3:39 PM // reply »
11,314 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 »
11,314 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.


Ram
Mar 16, 2010 at 12:04 PM // reply »
1 Comments

Thanks Ben, this was super helpful ..


Mar 17, 2010 at 9:24 AM // reply »
11,314 Comments

@Ram,

No problem - glad I could help.


Apr 16, 2010 at 10:10 PM // reply »
1 Comments

I am try to use this code with in a cfdiv submit and it is not working.

Can you please advice.


Jun 18, 2010 at 5:54 AM // reply »
4 Comments

Anyone tried japanese/chinese characters in filename. It works in FF, Safari and Chrome but IE returns a garbage characters. I used charset="shift_jis" and charset="utf-8".


Jul 15, 2010 at 6:12 AM // reply »
4 Comments

Nobody found any solution with this problem:
japanese/chinese characters in filename. It works in FF, Safari and Chrome but IE returns a garbage characters. I used charset="shift_jis" and charset="utf-8".

<cfheader name="content-disposition" needs to do some improvement, I saw this fix in php...


Jul 16, 2010 at 1:48 AM // reply »
4 Comments

Found some alternative way to display the filename with japanese/chinese characters

<cfset str_file_name="????????.txt">
<cfset is_ie=IIF(findnocase("MSIE", cgi.HTTP_USER_AGENT,1) is 0, de("false"), de("true"))>
<cfif is_ie>
<cfset str_file_ext=listlast(str_file_name, '.')>
<cfset str_file_title=replaceNoCase(str_file_name, "#str_file_ext#", "", "all")>
<cfset str_file_title=replaceNoCase(str_file_title, ".", "", "all")>
<cfheader name="content-disposition" charset="utf-8" value= "attachment; filename =#chr(34)##URLEncodedFormat('#str_file_title#')#.#str_file_ext##chr(34)#;" />
<cfelse>
<cfheader name="content-disposition" charset="utf-8" value= "attachment; filename =#chr(34)##str_file_name##chr(34)#;" />
</cfif>


Jul 16, 2010 at 10:44 AM // reply »
11,314 Comments

@Nelgraine,

Interesting - you are url-encoding the file name. I have never tried that before.


Don
Oct 4, 2010 at 4:21 PM // reply »
57 Comments

Here is my question, where can I find everything that cfheader can do for me? I've found bits and pieces but there must be more. Do I have to look for just headers or what? My googling fingers are wearing out.


Oct 4, 2010 at 8:45 PM // reply »
11,314 Comments

@Don,

There's not inherent limit to what you can send in headers. I think the only technical limit is that headers can't have line-breaks (otherwise the client interprets the post-line-break content as a new header value or the body)... but I might be off-base on that assumption.

So, you can send any application-specific headers that you like.

That said, there are a number of very common headers that have to do with things like caching/expiration, redirections, authorization (oAuth, Basic, etc.), content length, content type, content disposition (as demonstrated in this blog post).

Is there something you were looking for in particular? Or were you just getting ideas for what was capable?


Don
Oct 5, 2010 at 9:54 AM // reply »
57 Comments

@Ben,
Primarily I was looking for what is available. Another one of those times when I was looking at one thing and thought "what else?" I am doing 2 things, no caching and force the download box to come up for PDFs.
I finally found a place that showed a lot of what is routinely done with headers such as size etc. Not sure how much of it is really useful except the caching stuff.


Oct 5, 2010 at 9:57 PM // reply »
11,314 Comments

@Don,

One of the most important headers that you can set it he content-type header, which can be set implicitly using the CFContent tag. Other than that, I really only use headers an as-needed basis.


Apr 24, 2013 at 4:28 PM // reply »
1 Comments

I am working on something like skybox, I am trying to use

  • cfheader name="Content-Disposition" value="attachment;

to allow the user to download a file but how would I use this if their is more than one file.

What ever I do result in only one file being downloaded.

Also. Thanks for making this site available. It real help when I get stuck.



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
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
Jun 18, 2013 at 3:39 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
Hi Ben, THANKS! While not bleeding edge, it is new to me & I like learning new things every day! ... read »
Jun 18, 2013 at 12:30 PM
Disabling Auto-Correct And Auto-Capitalize Features On iPhone Inputs
Also spellcheck="false" should be mentioned as part of html5 specs ... read »
Jun 18, 2013 at 8:40 AM
Using Named Functions Within Self-Executing Function Blocks In Javascript
Hi Ben, you forgot to mention the most important thing for named self-executing functions - they can be referenced by name ONLY inside their execution context (which is parens in this case), it mean ... read »
dee
Jun 18, 2013 at 7:01 AM
My Safari Browser SQLite Database Hello World Example
hai ben, this program is really good i could understand the concept but i dint know how to save it and how to open it as you have done in the video can u give that details pls ... read »
Jun 18, 2013 at 6:04 AM
Clearing Inline CSS Properties With jQuery
Thanks a lot for for post! It helped me a lot... after being stuck since 24 hrs.. found solution from your post. Thanks again! ... read »
Jun 18, 2013 at 2:31 AM
SOTR 2013 - The Best Conference I Never Went To
I keep watching it, should keep me happily distracted until SotR14 ;) ... read »
Jun 17, 2013 at 9:45 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, As I was reading what you wrote, it occurred to me that maybe I do something similar to that in some of my client-side code. In an application I'm working on, there are a bunch of unrelated ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools