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 »
10,640 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 »
10,640 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 »
10,640 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 »
10,640 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 »
2 Comments

Thanks a ton Ben!!!

Google + Ben Nadel save the day again!


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



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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »