Adding Attachments To A CFPresentation

Posted By : todd sharp Posted At : May 30, 2008 2:03 PM Posted In: ColdFusion

5

The other day my buddy Ray suggested that I allow attachments to presentations on SlideSix. I thought it was a great idea, but when I checked the docs for <cfpresentation> I was surprised to find that the tag did not support attachments out of the box. However, after a bit of hackery I found a way to do it.

When you create a preso with cfpresentation and write it to disk there is a subdirectory created called 'data'. Within the 'data' directory are a handful of XML config files and whatnot, and a few of these files are the key to adding attachments. The first mod needed is to enable the presentation UI button that indicates that there are attachments. Doing this is as simple as modding the vconfig.xml file and changing this:

<uishow name="attachments" value="false"/>

To:

<uishow name="attachments" value="true"/>

Once you've done this and have reloaded your generated presentation (clear your cache if necessary) you will see a paper clip attachment indicating that there are attachments (although we haven't added any yet).

To add the actual attachments you'd need to modify the 'viewer.xml' file and add an <attachments> block (wow go figure, who woulda thunk of that name?). The attachments block goes inside the <presentation> node - I put mine below the <slides> block (not sure if the position matters or not). The attachments block takes 1-n <attachment> children - each of which take a url and name attribute. The url points to the attached file and the name is the text label that is used to link to the attachment. Here's how my attachments block looks:

<attachments>
    <attachment name="test with attachment4" url="http://mysite.com/index.cfm?event=attachment.download&slideShowID=1"/>
</attachments>

If you look at the image above you'll see what the attachment looks like after saving the changes to viewer.xml. I decided it was best to point to a CFM page that serves the attachment up with <cfcontent>. And that's it, attachments added to your cfpreso.

It's probably worth noting that you'd probably be better off using CF to make these changes since you'd probably not want to manually change these settings every time you write your cfpresentation to disk. Since CF can easily modify XML there's no reason not to automate the changes with a bit of code. Here is what my code looks like to do that:

<!--- read in the xml files --->
<cffile action="read" file="#viewer#" variable="viewerXML" />
<cffile action="read" file="#vconfig#" variable="vconfigXML" />

<!--- parse the xml --->
<cfset viewerXML = xmlParse(viewerXML) />
<cfset vconfigXML = xmlParse(vconfigXML) />

<!--- get the node that contains the 'attachment' value --->
<cfset vconfigAtt = xmlSearch(vconfigXML,"//configuration/layout/uishow[@name='attachments']") />
<!--- set attachments to 'true' so the UI displays the paper clip icon --->
<cfset vconfigAtt[1].XMLAttributes.value = "true" />

<!--- add an 'attachments' block to the viewer xml --->
<cfset viewerXML.presentation.XMLChildren[arrayLen(viewerXML.presentation.XMLChildren)+1] = xmlElemNew(viewerXML,"attachments") />
<!--- add an 'attachment' block to the 'attachments' block --->
<cfset viewerXML.presentation.attachments.XMLChildren[arrayLen(viewerXML.presentation.attachments.XMLChildren)+1] = xmlElemNew(viewerXML, "attachment") />

<!--- set the url where the attachment is served from --->
<cfset viewerXML.presentation.attachments.attachment.XMLAttributes["url"] = "http://myurl" />
<cfset viewerXML.presentation.attachments.attachment.XMLAttributes["name"] = "text to be displayed" />

<!--- write the xml back to the files --->
<cffile action="write" file="#viewer#" output="#toString(viewerXML)#" />
<cffile action="write" file="#vconfig#" output="#toString(vconfigXML)#" />

Comments (5)

Rupesh's Gravatar Excellent detective work.. Todd! We knew people would find a way to do this and that is why we didnt do it ;-)

todd sharp's Gravatar Thanks Rupesh. I should have also mentioned that using an <external> block under attachments lets you point to an external url and that <local> does the same thing as <attachment>. Wonder what other hidden goodies are in CF8??

JoeRav's Gravatar Is this still possible using the cf 8 Developer edition, or does it work in standard and enterprise versions only. I can't seem to find the data directory you are mentioning.

todd sharp's Gravatar Yes it should work for you. You don't see a data directory as part of the files that are written to disk for your cfpresentation?

JoeRav's Gravatar No, my file is right under the wwwroot directory.Ii don't see any children data folders. Could it be placing this folder somewhere else?