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
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...
AI and machine learning disruption for Enterprises started happening in the areas such as IT operations management (ITOPs) and Cloud management and SaaS apps. In 2019 CIOs will see disruptive solutions for Cloud & Devops, AI/ML driven IT Ops and Cloud Ops. Customers want AI-driven multi-cloud operations for monitoring, detection, prevention of disruptions. Disruptions cause revenue loss, unhappy users, impacts brand reputation etc.
The Japan External Trade Organization (JETRO) is a non-profit organization that provides business support services to companies expanding to Japan. With the support of JETRO's dedicated staff, clients can incorporate their business; receive visa, immigration, and HR support; find dedicated office space; identify local government subsidies; get tailored market studies; and more.
As you know, enterprise IT conversation over the past year have often centered upon the open-source Kubernetes container orchestration system. In fact, Kubernetes has emerged as the key technology -- and even primary platform -- of cloud migrations for a wide variety of organizations. Kubernetes is critical to forward-looking enterprises that continue to push their IT infrastructures toward maximum functionality, scalability, and flexibility. As they do so, IT professionals are also embr...
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...
Atmosera delivers modern cloud services that maximize the advantages of cloud-based infrastructures. Offering private, hybrid, and public cloud solutions, Atmosera works closely with customers to engineer, deploy, and operate cloud architectures with advanced services that deliver strategic business outcomes. Atmosera's expertise simplifies the process of cloud transformation and our 20+ years of experience managing complex IT environments provides our customers with the confidence and trust tha...
As you know, enterprise IT conversation over the past year have often centered upon the open-source Kubernetes container orchestration system. In fact, Kubernetes has emerged as the key technology -- and even primary platform -- of cloud migrations for a wide variety of organizations. Kubernetes is critical to forward-looking enterprises that continue to push their IT infrastructures toward maximum functionality, scalability, and flexibility.
Today's workforce is trading their cubicles and corporate desktops in favor of an any-location, any-device work style. And as digital natives make up more and more of the modern workforce, the appetite for user-friendly, cloud-based services grows. The center of work is shifting to the user and to the cloud. But managing a proliferation of SaaS, web, and mobile apps running on any number of clouds and devices is unwieldy and increases security risks. Steve Wilson, Citrix Vice President of Cloud,...
When Enterprises started adopting Hadoop-based Big Data environments over the last ten years, they were mainly on-premise deployments. Organizations would spin up and manage large Hadoop clusters, where they would funnel exabytes or petabytes of unstructured data.However, over the last few years the economics of maintaining this enormous infrastructure compared with the elastic scalability of viable cloud options has changed this equation. The growth of cloud storage, cloud-managed big data e...
Artificial intelligence, machine learning, neural networks. We're in the midst of a wave of excitement around AI such as hasn't been seen for a few decades. But those previous periods of inflated expectations led to troughs of disappointment. This time is (mostly) different. Applications of AI such as predictive analytics are already decreasing costs and improving reliability of industrial machinery. Pattern recognition can equal or exceed the ability of human experts in some domains. It's devel...