7 Replies Latest reply on Mar 14, 2007 11:33 PM by Newsgroup_User

    cffile accept

    craig_mac Level 1
      Hi i have a file upload which i need to only accept CSV files, i have 2 problems with the code below

      1. i have tried uploading a csv file but i get an error, do i need to add more to the accept clause?

      2. if a user uploads a differnent mime type how can i do a <cfif file ext is not csv redirection>

      <cffile action="upload" filefield="FILE" destination=#currentdir# nameconflict="makeunique" accept="CSV">
        • 1. Re: cffile accept
          Level 7
          formal mime type for csv files is text/csv
          so change accept="CSV" to accep="text/csv"
          --

          Azadi Saryev
          Sabai-dee.com
          http://www.sabai-dee.com
          • 2. Re: cffile accept
            craig_mac Level 1
            ok thanks,

            how do i now use a cfif tag to redirect the user if he tries to upload a jpeg etc,
            • 3. Re: cffile accept
              Level 7
              craiglaw98 wrote:
              > ok thanks,
              >
              > how do i now use a cfif tag to redirect the user if he tries to upload a jpeg etc,

              if you want to do it in CF, then you will have to use <cftry>/<cfcatch>
              structure to catch the error cffile will trow when a user tries to
              submit a wrong mime type file. i don't quiet remember which exception is
              thrown (i think it is application) - will look it up at work and post
              back...

              --
              Azadi Saryev
              Sabai-dee.com
              Vientiane, Laos
              http://www.sabai-dee.com
              • 4. Re: cffile accept
                craig_mac Level 1
                ok thanks

                i found this code, do this help

                Error Handling File Manipulations

                Now, as I discussed in my error handling article, file manipulations can be sketchy creatures. Sometimes they don't work as we intend them to. They can also be very, very dangerous. So, I recommend wrapping each and every file manipulation in it's own try/catch block. (Don't package two of them in the same block, it will be much more difficult to trace, believe me. It's worth the little extra bit of effort.)

                <cftry>
                <cffile action="read" file="#newDir#test1.txt" variable="myVar">
                <cfcatch>
                <!--- genius bit of error logging here --->
                </cfcatch>
                </cftry>
                • 5. Re: cffile accept
                  Level 7
                  yes, that's exactly what you should use. i have checked and the wrong
                  mime type throws exception of application type, so:

                  <cftry>
                  <cffile action="upload" destination="somefolder"
                  filefield="form.yourfilefieldnamehere" accept="text/csv"
                  nameconflict="whichever_option_you_use">
                  <cfcatch type="application">
                  your wrong file type handling code goes here. depends on how you want to
                  handle it: show an error message on the same page and cancel form
                  submitting or redirect to some error page or whaever you want to do.
                  </cfcatch>
                  </cftry>
                  --

                  Azadi Saryev
                  Sabai-dee.com
                  http://www.sabai-dee.com
                  • 6. Re: cffile accept
                    craig_mac Level 1
                    that works ok, accept, i get the error when i actually upload a .csv file?

                    i created a csv file from micosoft excel, and it will not allow it to be uploaded any ideas why?
                    • 7. Re: cffile accept
                      Level 7
                      if it was created from excel, then maybe it has excel mime type even
                      though it is a csv file... try using in your <cffile>:

                      accept="text/csv,application/msexcel"

                      or maybe accept="text/csv,application/csv" will work too...

                      these are mime type associated with csv files:
                      text/comma-separated-values
                      text/csv
                      application/csv
                      application/excel
                      application/vnd.ms-excel
                      application/vnd.msexcel
                      text/anytext

                      (reference: http://filext.com/detaillist.php?extdetail=CSV)

                      all of them should work. since csv files can be created with various
                      applications, exact mime tupe of a csv file will probably depend on the
                      application it was created with... i am just guessing here, though...

                      hope this helps

                      --

                      Azadi Saryev
                      Sabai-dee.com
                      http://www.sabai-dee.com