You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

The Trouble with Macs

Make your upload problems disappear

Some of us have trouble just trying to get CFFILE to upload an image correctly and put it in its proper place, without having a Macintosh user report that it doesn't work for him or her. Diligently searching the Macromedia ColdFusion forums provided the base solutions for me, and I packaged those fixes with my own touches to create a custom file upload tag for you.

I realize that there are a number of areas where Windows developers pull their hair out when it comes time to test the code on a Mac. I don't have the solution for all of the problems I read about on the forums, but I do have fixes for two of them. This article will tackle problems with comparing strings and with uploading files.

Recognizing Your Options
How many times have you coded something like this?

<cfparam name="option" default="">
<cfswitch expression="#option#">
<cfcase value="EDIT">
...your code here...
...more case statements here...

Works perfectly fine on your Windows machine, right? This will fail for certain Mac users because the "option" will be totally ignored.

The solution? UCASE(TRIM()). Get rid of the extra spaces, which are significant for some Mac versions, and compare case to case, and these comparison problems will disappear:

<cfparam name="option" default="">
<cfswitch expression="#UCASE(TRIM(option))#">
<cfcase value="EDIT">
...your code here...
...more case statements here...

When in doubt, UCASE(TRIM()) the variables. A miniscule performance hit is nothing compared to the wrath of a customer who can't make your program work.

Uploading Files: the CF_UploadHelper Custom Tag
The problem of uploading files to a Mac was the subject of many forum messages. Through trial and error, I picked the best solutions from the bunch and combined them into a custom tag (see Listing 1: UploadHelper.cfm). (Code listings for this article can be downloaded from www.sys-con.com/coldfusion/sourcec/cfm.)

I purposely made UploadHelper as generic as possible so you wouldn't have to mess with it at all if you didn't want to. Those of you who want to further customize it, be my guest. As always, I welcome any suggestions for improving this tag - which is also now available on the Macromedia Developer Exchange.

The beginning of the tag does the customary checking to ensure that the minimum parameters have been passed. Variables are then set for acceptable MIME types that you want to allow to be uploaded, along with a list of file extensions to display to the user (if desired) upon failure.

Mapping Makes Life Easier
I'm now in the habit of using a global shared directory area for managing these types of files for all clients, and I mapped that directory using ColdFusion's Administrator. This makes life easier not only for uploading and managing files, but also for providing a common repository for global INCLUDE files, display templates, error-handling code, images, and other items that you can and do use with more than one client.

If you are managing a number of clients, mapping a common directory will help you manage uploaded files. I suggest that you have a separate subdirectory for each client under this mapped directory (e.g., /sharedfiles/client1/). You will likely find many more uses for this common mapped directory.

Testing the Tag
UploadHelper was developed to allow you to manage your uploaded files, including images, documents, or whatever types of files your customer needs to manage. UploadHelper allows you to let your site visitor safely delete files from the server, manage file captions, and provide left/right alignment for images. A test script has been provided for you in Listing 2: ImgTagTest.cfm.

You will need to set the common variables to the appropriate values and ensure a table has been set up with the proper fields in order for this program to work. For now, create a test table and manually add an entry or two into it so that something shows up in your test list when you run ImgTagTest.cfm. Make sure you have a copy of UploadHelper.cfm either in your custom tags directory or in the same directory that ImgTagTest.cfm is in.

An example of the EDIT screen from ImgTagTest.cfm can be found in Figure 1. This example shows a thumbnail of the image to users so they can be sure they're editing, replacing, or deleting the correct file. UploadHelper allows your site visitor to delete a file or change the caption without uploading a new file, or upload a file without changing the caption. Of course, all three can be performed at the same time as well, and if a file exists when a new file is uploaded, the previous file will be deleted to keep your server from bloating due to orphaned files.


The Meat of the Matter
Why does this tag work? Why does your code fail? Some versions of Mac will report that a file has been uploaded even if you never touched the browse button to go look for a file. The CFFILE-ReadBinary command checks to see if the file is a valid one, and therefore weeds out the false alarms.

The Mac then throws a double-whammy at you by letting people manage their files without a proper file extension. The file may very well be valid, but without an extension, your Windows server hasn't a clue as to how to process it. The TRY/CATCH wrapper around the second CFFILE command with the ACCEPT list of types allows you to make a clean getaway when the file extension is not an approved one.

There used to be a tag that would read the file type, regardless of whether there is an extension or not: cf_imagesize. You can use this tag if you have it, but it's no longer available on the Developer Exchange. I checked the Web site of the publishing company (Fuseware), but they don't appear to be active.

Rather than send you on a wild goose chase for this tag, I incorporated what was necessary into CF_UPLOADHELPER, and you can use it by simply setting the USEIMAGESIZE attribute to "YES". This will now let you upload those pesky, no-extension Mac files - as long as they are GIF or JPG files - without making your user go back and put an extension on and re-attempt the upload.

CF_UPLOADHELPER is a huge tag at 446 lines, no doubt about it. You can certainly shrink it by incorporating CFMX techniques and by deciding on some default behavior rather than allowing all sorts of possibilities, as I did.

One thing is for certain, though. If you use CF_UPLOADHELPER, your Mac file upload problems will disappear and you won't have to reinvent the wheel every time you want to safely handle file uploads in your applications.

More Stories By Randy Smith

Randy L. Smith is president/CEO of Midwest Computer Programming and Internet (www.mcpi.com), an Internet/intranet database solution provider based in Hudson, Wisconsin. He has been developing large-scale, Web-based applications for businesses and nonprofits of all sizes, as well as state and federal entities, since 1993. Randy has been working in the computer industry since 1978, and with ColdFusion since 1996.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

IoT & Smart Cities Stories
Dion Hinchcliffe is an internationally recognized digital expert, bestselling book author, frequent keynote speaker, analyst, futurist, and transformation expert based in Washington, DC. He is currently Chief Strategy Officer at the industry-leading digital strategy and online community solutions firm, 7Summits.
Digital Transformation is much more than a buzzword. The radical shift to digital mechanisms for almost every process is evident across all industries and verticals. This is often especially true in financial services, where the legacy environment is many times unable to keep up with the rapidly shifting demands of the consumer. The constant pressure to provide complete, omnichannel delivery of customer-facing solutions to meet both regulatory and customer demands is putting enormous pressure on...
IoT is rapidly becoming mainstream as more and more investments are made into the platforms and technology. As this movement continues to expand and gain momentum it creates a massive wall of noise that can be difficult to sift through. Unfortunately, this inevitably makes IoT less approachable for people to get started with and can hamper efforts to integrate this key technology into your own portfolio. There are so many connected products already in place today with many hundreds more on the h...
The standardization of container runtimes and images has sparked the creation of an almost overwhelming number of new open source projects that build on and otherwise work with these specifications. Of course, there's Kubernetes, which orchestrates and manages collections of containers. It was one of the first and best-known examples of projects that make containers truly useful for production use. However, more recently, the container ecosystem has truly exploded. A service mesh like Istio addr...
Digital Transformation: Preparing Cloud & IoT Security for the Age of Artificial Intelligence. As automation and artificial intelligence (AI) power solution development and delivery, many businesses need to build backend cloud capabilities. Well-poised organizations, marketing smart devices with AI and BlockChain capabilities prepare to refine compliance and regulatory capabilities in 2018. Volumes of health, financial, technical and privacy data, along with tightening compliance requirements by...
Charles Araujo is an industry analyst, internationally recognized authority on the Digital Enterprise and author of The Quantum Age of IT: Why Everything You Know About IT is About to Change. As Principal Analyst with Intellyx, he writes, speaks and advises organizations on how to navigate through this time of disruption. He is also the founder of The Institute for Digital Transformation and a sought after keynote speaker. He has been a regular contributor to both InformationWeek and CIO Insight...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
To Really Work for Enterprises, MultiCloud Adoption Requires Far Better and Inclusive Cloud Monitoring and Cost Management … But How? Overwhelmingly, even as enterprises have adopted cloud computing and are expanding to multi-cloud computing, IT leaders remain concerned about how to monitor, manage and control costs across hybrid and multi-cloud deployments. It’s clear that traditional IT monitoring and management approaches, designed after all for on-premises data centers, are falling short in ...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...