5

I have a form which allows a user to upload a file to the server. How can I validate that the uploaded file is in fact the expected format (CSV, or at least validate that it is a text file) in ColdFusion 8?

Eric Belair
  • 10,574
  • 13
  • 75
  • 116
  • 2
    You'll need to use Java and there doesn't appear to be a definitive approach. See this question: http://stackoverflow.com/questions/51438/getting-a-files-mime-type-in-java – orangepips Jun 01 '11 at 20:05

3 Answers3

4

For simple formats like CSV, just check yourself, for example via regex.

 <cffile action="read" file="#uploadedFile#" variable="contents" charset="UTF-8">

 <cfset LooksLikeCSV = REFind("^([^;]*;)+[^;]*$", contents)>

You can place additional checks with regard to file size limits or forbidden characters.

For other file formats, you can check for header signatures that occur in the first few bytes of the file.

You could even write a full parser for your expected file format - for CSV validation, you could do a ListToArray() at CR/LF and check each line individually against a regex. XML should work pretty straightforward as well - just try to pass it to XmlParse(). Binary formats like images are a little more difficult, but libraries exist there as well.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
0

I dont know if it can help you but Ben Nadel wrote excellents posts about CSV:

http://www.bennadel.com/blog/483-Parsing-CSV-Data-Using-ColdFusion.htm

http://www.bennadel.com/blog/976-Regular-Expressions-Make-CSV-Parsing-In-ColdFusion-So-Much-Easier-And-Faster-.htm

http://www.bennadel.com/blog/501-Parsing-CSV-Values-In-To-A-ColdFusion-Query.htm

LarZuK
  • 746
  • 1
  • 8
  • 16
0

I think it's as simple as specifying the accept value in cffile ...Unfortunately the CF8 docs don't specify the value as part of the info for cffile ... It's under file management ...

<cffile action=”upload” filefield=”filename” destination=”#destination#” accept=”text/csv”>

CF8 » Controlling the type of file uploaded

Edward J Beckett
  • 5,061
  • 1
  • 41
  • 41