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
Intel is an American multinational corporation and technology company headquartered in Santa Clara, California, in the Silicon Valley. It is the world's second largest and second highest valued semiconductor chip maker based on revenue after being overtaken by Samsung, and is the inventor of the x86 series of microprocessors, the processors found in most personal computers (PCs). Intel supplies processors for computer system manufacturers such as Apple, Lenovo, HP, and Dell. Intel also manufactu...
Darktrace is the world's leading AI company for cyber security. Created by mathematicians from the University of Cambridge, Darktrace's Enterprise Immune System is the first non-consumer application of machine learning to work at scale, across all network types, from physical, virtualized, and cloud, through to IoT and industrial control systems. Installed as a self-configuring cyber defense platform, Darktrace continuously learns what is ‘normal' for all devices and users, updating its understa...
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...
OpsRamp is an enterprise IT operation platform provided by US-based OpsRamp, Inc. It provides SaaS services through support for increasingly complex cloud and hybrid computing environments from system operation to service management. The OpsRamp platform is a SaaS-based, multi-tenant solution that enables enterprise IT organizations and cloud service providers like JBS the flexibility and control they need to manage and monitor today's hybrid, multi-cloud infrastructure, applications, and wor...
The Master of Science in Artificial Intelligence (MSAI) provides a comprehensive framework of theory and practice in the emerging field of AI. The program delivers the foundational knowledge needed to explore both key contextual areas and complex technical applications of AI systems. Curriculum incorporates elements of data science, robotics, and machine learning-enabling you to pursue a holistic and interdisciplinary course of study while preparing for a position in AI research, operations, ...
CloudEXPO has been the M&A capital for Cloud companies for more than a decade with memorable acquisition news stories which came out of CloudEXPO expo floor. DevOpsSUMMIT New York faculty member Greg Bledsoe shared his views on IBM's Red Hat acquisition live from NASDAQ floor. Acquisition news was announced during CloudEXPO New York which took place November 12-13, 2019 in New York City.
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...
Tapping into blockchain revolution early enough translates into a substantial business competitiveness advantage. Codete comprehensively develops custom, blockchain-based business solutions, founded on the most advanced cryptographic innovations, and striking a balance point between complexity of the technologies used in quickly-changing stack building, business impact, and cost-effectiveness. Codete researches and provides business consultancy in the field of single most thrilling innovative te...
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...
With the introduction of IoT and Smart Living in every aspect of our lives, one question has become relevant: What are the security implications? To answer this, first we have to look and explore the security models of the technologies that IoT is founded upon. In his session at @ThingsExpo, Nevi Kaja, a Research Engineer at Ford Motor Company, discussed some of the security challenges of the IoT infrastructure and related how these aspects impact Smart Living. The material was delivered interac...