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
Moroccanoil®, the global leader in oil-infused beauty, is thrilled to announce the NEW Moroccanoil Color Depositing Masks, a collection of dual-benefit hair masks that deposit pure pigments while providing the treatment benefits of a deep conditioning mask. The collection consists of seven curated shades for commitment-free, beautifully-colored hair that looks and feels healthy.
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
We all love the many benefits of natural plant oils, used as a deap treatment before shampooing, at home or at the beach, but is there an all-in-one solution for everyday intensive nutrition and modern styling?I am passionate about the benefits of natural extracts with tried-and-tested results, which I have used to develop my own brand (lemon for its acid ph, wheat germ for its fortifying action…). I wanted a product which combined caring and styling effects, and which could be used after shampo...
The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected pat...
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Druva is the global leader in Cloud Data Protection and Management, delivering the industry's first data management-as-a-service solution that aggregates data from endpoints, servers and cloud applications and leverages the public cloud to offer a single pane of glass to enable data protection, governance and intelligence-dramatically increasing the availability and visibility of business critical information, while reducing the risk, cost and complexity of managing and protecting it. Druva's...
BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.