Welcome!

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...
</cfcase>
...more case statements here...
</cfswitch>

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...
</cfcase>
...more case statements here...
</cfswitch>

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.

CF_ImageSize
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.

Conclusion
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
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...
Nicolas Fierro is CEO of MIMIR Blockchain Solutions. He is a programmer, technologist, and operations dev who has worked with Ethereum and blockchain since 2014. His knowledge in blockchain dates to when he performed dev ops services to the Ethereum Foundation as one the privileged few developers to work with the original core team in Switzerland.
René Bostic is the Technical VP of the IBM Cloud Unit in North America. Enjoying her career with IBM during the modern millennial technological era, she is an expert in cloud computing, DevOps and emerging cloud technologies such as Blockchain. Her strengths and core competencies include a proven record of accomplishments in consensus building at all levels to assess, plan, and implement enterprise and cloud computing solutions. René is a member of the Society of Women Engineers (SWE) and a m...
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...
Whenever a new technology hits the high points of hype, everyone starts talking about it like it will solve all their business problems. Blockchain is one of those technologies. According to Gartner's latest report on the hype cycle of emerging technologies, blockchain has just passed the peak of their hype cycle curve. If you read the news articles about it, one would think it has taken over the technology world. No disruptive technology is without its challenges and potential impediments t...
If a machine can invent, does this mean the end of the patent system as we know it? The patent system, both in the US and Europe, allows companies to protect their inventions and helps foster innovation. However, Artificial Intelligence (AI) could be set to disrupt the patent system as we know it. This talk will examine how AI may change the patent landscape in the years to come. Furthermore, ways in which companies can best protect their AI related inventions will be examined from both a US and...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (November 12-13, 2018, New York City) today announced the outline and schedule of the track. "The track has been designed in experience/degree order," said Schmarzo. "So, that folks who attend the entire track can leave the conference with some of the skills necessary to get their work done when they get back to their offices. It actually ties back to some work that I'm doing at the University of San...
When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science," is responsible for setting the strategy and defining the Big Data service offerings and capabilities for EMC Global Services Big Data Practice. As the CTO for the Big Data Practice, he is responsible for working with organizations to help them identify where and how to start their big data journeys. He's written several white papers, is an avid blogge...