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 »
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:
| | | | ||
| | ![]() | | ||
| | | |
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
Comments (8) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Learning ColdFusion 8: CFZip Part III - Reading Zip File Entries
Exploring Path Info And How It Interacts With ColdFusion And The Browser
I think you have triple quotes at the end there...
Posted by Sami Hoda on Jul 19, 2007 at 1:28 AM
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 ?
Posted by duncan on Jul 19, 2007 at 5:10 AM
@Duncan,
I just tried it and the %20 actually shows up in the file name.
Posted by Ben Nadel on Jul 19, 2007 at 7:38 AM
Is this actually ColdFusion doing this, or the browser?
Posted by Peter Boughton on Jul 20, 2007 at 4:48 AM
@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.
Posted by Ben Nadel on Jul 20, 2007 at 6:57 AM
You're welcome Ben!
@Peter
It's the browser. http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download
Posted by Elliott Sprehn on Jul 31, 2007 at 1:34 PM
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
Posted by JC on Nov 5, 2007 at 3:26 PM
@JC,
Sorry, no ideas. I haven't even upgraded to IE7 yet, so I wouldn't even know how to test.
Posted by Ben Nadel on Nov 5, 2007 at 3:59 PM