Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

File Upload Validation

Explorer ,
Jul 13, 2009 Jul 13, 2009

Hi All,

I have a form with an element of input type file to upload a file from the user's box to the server's box, however after clicking upload I need to perform a couple of validations. What I need actually from this form is the filename that is to be uploaded (emphasis on 'that is to be uploaded' and not 'uploaded').

For example:

1. I need to validate that the filename does not contain spaces before writing it to the server's file system.

2. Has a certain extension name...

Since it's only after an upload that CF provides certain return vars like serverFileName..., it's not possible to retrieve the filename before upload. Trying to get the filename from the form is also not possible 'cos the name assigned after CF processed the form is of name pattern "tmp-02.upload" 02 being some random generated number, actual filename is "documentanalysis.jrxml". Don't want to use Javascript as this is unreliable since it can be disabled (this app is an intranet app). Other CFFILE action types don't provide return vars (thought of a workaround using another action type).

Could anyone please help or point me in the right direction to get this resolved? Thanks.

1.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 14, 2009 Jul 14, 2009

Hi Seyi103



Getting the file name before upload is not much possible. I have tried the same and haven got any solution. But what you can do is you can remove the space from the file name after you upload it by renaming it (If that solves your problem).
Flash forms provide a better features like allowing files of certain extensions to be browsed for upload (Filtering). I would suggest to use the flash file upload

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 14, 2009 Jul 14, 2009

You'll need to use JavaScript to perform the validation client side

(listen for the onchange event on the input element), ColdFusion is

not of much use here because it executes right after the browser

finished the file upload.

Mack

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 14, 2009 Jul 14, 2009

Hi,

   1) To remove the space, you can   use the 'trim' function while inserting into database as trim(file.serverfile)

2)To upload files with certain extension name use accept field as in  below example

<cffile destination="#destinationdirectory#" action="upload" nameconflict="makeunique" filefield="uploadfieldname" accept="text/*,application/msword,application/rtf,application/pdf,text/html">

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 14, 2009 Jul 14, 2009

We have an intranet app that relies on js.  We have some code in the application.cfm that checks the availability of js on the user machine.  If they have it disabled, they don't get access to the app.

You might consider the same approach.  Then you can use js to validate your form.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 14, 2009 Jul 14, 2009

@ Dan

Thanks, could you please post a snippet of the code in your Application.cfm file to check if js is enabled on the client browser.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 14, 2009 Jul 14, 2009

There is a lot of variable setting going on, but the gist of it is this.

On the first visit to any page in the app, checks are made to ensure the user has js enabled and will accept cookies.  Specifically,

cfcookie is used with cftry to write a cf cookie.

js is used to write a js cookie.

meta refresh is used to reload the page.

Then cf attempts to read the two cookies.  If both are not read, the user gets a message telling him to do something about what was found.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 14, 2009 Jul 14, 2009
LATEST

Thanks Dan, would find a workaround. Thanks all for your input.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources