July 3, 2009

Pages


Search Site


Subscribe


Topics



Archives

File upload gotcha: PDFs get different mime types per browser

July 18 2008 by Adam

I've had some time over the last few days to start adding some of the "nice to have" features to a client site before delivering it, and one of those was the ability to upload product images and related PDFs, rather than having to teach them to always follow the naming scheme and how to use FTP. While testing my uploads in various browsers, I came across something I found to be very odd.

When you upload a PDF in IE, the file you receive has what I believe is the correct mime type: "application/pdf" however when you upload the same PDF file using the same code, in Firefox (tested in FF 2 and 3), the mime type is "application/download". I can't explain it. I have no idea why it's wrong. But it is, and consistently so. Could this be the one thing that is implemented correctly in IE and incorrectly in Firefox?

At any rate, it's a simple thing to accommodate. Note the accept attribute of the <CFFile> tag in the code sample below:

<cftry>
   <cffile action="upload" accept="application/download,application/pdf" filefield="form.pdfUpload" nameconflict="overwrite" result="result" destination="#expandPath('/path/to/save/your.pdf')#" />
   <cfif variables.result.fileExisted neq "NO">
      <cfset msg = "PDF was overwritten." />
   <cfelse>
      <cfset msg = "PDF was saved." />
   </cfif>
   <cfcatch type="application">
      <cfif findNoCase("The MIME type of the uploaded file", cfcatch.message)>
         <!--- invalid mime type --->
         <cfset msg = "Invalid file type. Please only use a PDF file. File was not saved." />
      </cfif>
   </cfcatch>
</cftry>

Posted in ColdFusion | 17 comments

No Tweetbacks

    17 comments:

    Leave a Reply





    Leave this field empty: