A reader ran into an interesting issue:
I was working with CFIMAGE tag. Lets say
<cfimage source=”#VARIABLES.myImage#” name=”VARIABLES.isLargeImage”>
This works perfect, but when I have space in my image path like
<cfset VARIABLES.myImage=”http://somesite.com/image Folder/image.jpg”>
It throws error. My path comes dynamically from DB in actual code.
I also tried urlEncodedFormat on my path but that did not worked too.
I think CF running on window server should be able enough to resolve paths which have spaces in its folder/file names.
To show you an example of what he said, I dropped an image with a space in it into a folder and then tried to cfimage it as he did:
When run, you get an error:
Interestingly enough, the browser has no issue with this. I’ve noticed this for years now – browsers (well at least Chrome) simply automatically escape the spaces for you.
If you try to fix it with URLEncodedFormat, it still fails. It is rather easy to see why if you output the result of the URL:
So to fix it, we need to urlEncode the file name. To do this we need to break apart the URL. There are multiple ways of doing this, but for simplicity sake, I used the parseURL UDF from CFLib.org. It uses string functions to break apart a URL into its components. As an example, this code…
Returns these values:
Based on what I saw here, I thought this might be a nice way to rebuild the URL. I would not call this code 100% safe as it wouldn’t pick up the username and password if they existed, but I wanted something simple.
And the result…
I hope this is helpful!