Reading a CSV or Pipe delimited file - coldfusion

I am trying to read a csv file that is using a | delimiter instead of ,.
I am using
<cffunction name="loadCSVfile" access="public" returntype="string">
<cfargument name="filename" required="yes" default="">
<cffile action="read" file="#filename#" variable="csvData">
<cfreturn csvData>
</cffunction>
which works fine with files that are , delimiter but does not work with |. Is there anything else that I need to do for this function to work?
This is how I'm calling the function:
<cfscript>
csvloaderCFC = CreateObject("component","AlscCSVloader");
</cfscript>
<cfset csvfilename = DataDirectory&q_DataDirectory.name>
<!--- backup CSV file ---->
<cffile action= "copy" source = "#csvfilename#"
destination = "#DataDirectorybackup#">
<!--- Load CSV data ---->
<cfscript>
csvData = csvloaderCFC.loadCSVfile(csvfilename);
</cfscript>
<cfdump var="#csvData#">
The cfdump in the end it doesn't show at all
Pipe-delimitted sample
Location|patient_ID|First Name|Last Name|
111|468454|Eric |Gallegos |
111|468457|kelsey|anderson|
10|468459|Rivenda|Kaur|
Comma-delimitted sample
Location,patient_ID,First Name,Last Name,
111,468454,Eric ,Gallegos ,
111,468457,kelsey,anderson,
10,468459,Rivenda,Kaur,

There is quite a bit more you have do do than just a CFFile call to get a CSV as a struct of some kind. CFFile foes not do this on it's own. (although strangely CFHttp does) Honestly your best bet may be to be to use a Java library. There are several available from Apache: http://commons.apache.org/csv/
These should give you back something fairly easy to deal with.
If you want a "pure" coldfusion CSV option you can look at this blog post for a specific impl:
http://www.bennadel.com/blog/483-Parsing-CSV-Data-Using-ColdFusion.htm
basically you need to read the entire content and then parse it.

Related

IsSpreadsheetFile function in ColdFusion

I searched for IsSpreadsheetFile on the internet and cannot find many results. The reason I'm interested with this function is due to an article posted by Raymond Camden: ColdFusion Sample Upload and Parse an Excel File
He uses IsSpreadsheetFile function to determine if a file is a spreadsheet or not. I tested it, it works when my file extension is .xls but it doesn't work when it is .xlsx.
I cannot find any more explanation on the IsSpreadsheetFile function in ColdFusion documentation other than from Raymond's blog. How can I validate a spreadsheet other than using the IsSpreadsheetFile function?
Update:
Here are portions of my code that leads to calling IsSpreadsheetFile function:
<CFIF MyFileExt NEQ "xls" AND MyFileExt NEQ "xlsx">
<CFLOCATION url="../index.cfm?p=home&err=Please upload excel only.">
<CFELSE>
<CFSET ExcelObj = cfcomponents.Val_EXCEL(session.groupname,load_year)>
<CFSET ValidationResult = ExcelObj.IsExcelFile('GL',InstDirPath,FileName)>
....... codes continue on here after calling IsExcelFile method...
</CFIF>
Here is the IsExcelFile method:
<CFFUNCTION name="IsExcelFile" hint="Validate file.">
<cfargument name="AppName" type="String" required="TRUE">
<cfargument name="InstFileDir" type="String" required="TRUE">
<cfargument name="InstFileName" type="String" required="TRUE">
<cfset PathToExcelFile = "#Trim(arguments.InstFileDir)#/#Trim(arguments.InstFileName)#">
<cfdump var="#PathToExcelFile#"><br>
<cfdump var="#isSpreadsheetFile(PathToExcelFile)#"><br>
<cfoutput># isSpreadSheetObject(PathToExcelFile)# NOT an Object</cfoutput>
<cfabort>
......
</CFFUNCTION>
Note: The files definitely have .xlsx and .xls extensions. I created an .xls file by re-saving the .xlsx file to Excel 97-2003 Workbook (.xls) from Excel Workbook (.xlsx).

Excluding items from a list in coldfusion by type

Is there a way to exclude certain items by filetype in a list in Coldfusion?
Background: I just integrated a compression tool into an existing application and ran into the problem of the person's prior code would automatically grab the file from the upload destination on the server and push it to the Network Attached Storage. The aim now is to stop their NAS migration code from moving all files to the NAS, only those which are not PDF's. What I want to do is loop through their variable that stores the names of the files uploaded, and exclude the pdf's from the list then pass the list onto the NAS code, so all non pdf's are moved and all pdf's uploaded remain on the server. Working with their code is a challenge as no one commented or documented anything and I've been trying several approaches.
<cffile action="upload" destination= "c:\uploads\" result="myfiles" nameconflict="makeunique" >
<cfset fileSys = CreateObject('component','cfc.FileManagement')>
<cfif Len(get.realec_transactionid)>
<cfset internalOnly=1 >
</cfif>
**This line below is what I want to loop through and exclude file names
with pdf extensions **
<cfset uploadedfilenames='#myfiles.clientFile#' >
<CFSET a_insert_time = #TimeFormat(Now(), "HH:mm:ss")#>
<CFSET a_insert_date = #DateFormat(Now(), "mm-dd-yyyy")#>
**This line calls their method from another cfc that has all the file
migration methods.**
<cfset new_file_name = #fileSys.MoveFromUploads(uploadedfilenames)#>
**Once it moves the file to the NAS, it inserts the file info into the
DB table here**
<cfquery name="addFile" datasource="#request.dsn#">
INSERT INTO upload_many (title_id, fileDate, filetime, fileupload)
VALUES('#get.title_id#', '#dateTimeStamp#', '#a_insert_time#', '#new_file_name#')
</cfquery>
<cfelse>
<cffile action="upload" destination= #ExpandPath("./uploaded_files/zip.txt")# nameconflict="overwrite" >
</cfif>
Update 6/18
Trying the recommended code helps with the issue of sorting out filetypes when tested outside of the application, but anytime its integrated into the application to operate on the variable uploadedfilenames the rest of the application fails and the multi-file upload module just throws a status 500 error and no errors are reported in the CF logs. I've found that simply trying to run a cfloop on another variable not related to anything in the code still causes it to error.
As per my understanding, you want to filter-out file names with a specific file type/extension (ex: pdf) from the main list uploadedfilenames. This is one of the easiest ways:
<cfset lFileNames = "C:\myfiles\proj\icon-img-12.png,C:\myfiles\proj\sample-file.ppt,C:\myfiles\proj\fin-doc1.docx,C:\myfiles\proj\fin-doc2.pdf,C:\myfiles\proj\invoice-temp.docx,C:\myfiles\proj\invoice-final.pdf" />
<cfset lResultList = "" />
<cfset fileExtToExclude = "pdf" />
<cfloop list="#lFileNames#" index="fileItem" delimiters=",">
<cfif ListLast(ListLast(fileItem,'\'),'.') NEQ fileExtToExclude>
<cfset lResultList = ListAppend(lResultList,"#fileItem#") />
</cfif>
</cfloop>
Using only List Function provided by ColdFusion this is easily done, you can test and try the code here. I would recommend you to wrap this code around a function for easy handling. Another way to do it would be to use some complex regular expression on the list (if you're looking for a more general solution, outside the context of ColdFusion).
Now, applying the solution to your problem:
<cfset uploadedfilenames='#myfiles.clientFile#' >
<cfset lResultList = "" />
<cfset fileExtToExclude = "pdf" />
<cfloop list="#uploadedfilenames#" index="fileItem" delimiters=",">
<cfif ListLast(ListLast(fileItem,'\'),'.') NEQ fileExtToExclude>
<cfset lResultList = ListAppend(lResultList,fileItem) />
</cfif>
</cfloop>
<cfset uploadedfilenames = lResultList />
<!--- rest of your code continues --->
The result list lResultList is copied to the original variable uploadedfilenames.
I hope I'm not misunderstanding the question, but why don't you just wrap all of that in an if-statement that reads the full file name? Whether the files are coming one by one or through a delimited list, it should be easy to work around.
<cfif !listContains(ListName, '.pdf')>
OR
<cfif FileName does not contain '.pdf'>
then
all the code you posted

How to get the temporary path of a file pulled with CFHTTP in Coldfusion?

I'm using Coldfusion8 and need to fetch images from a remote server, which I'm doing like this:
<cfhttp timeout="45" throwonerror="no" url="#variables.testFilePath#" method="get" useragent="Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12" getasbinary="yes" result="variables.objGet">
<cfset variables.objImage = ImageNew(variables.objGet.FileContent)>
I now need to save the image to Amazon S3, but the function I want to use:
<cfset result = s3.putObject(url.b,file.serverFile,file.contentType,'300',form.cacheControl,'30',form.acl,form.storageClass,form.keyName,GetTempDirectory())>
Requires the directory where my generated image can be found in.
Question:
Is there a way to get the directory of an image file pulled with cfhttp and converted to an image using imageNew? Or do I need to save the file to disk first? I also need to resize before storing, so I might not be able to get by without saving to disk first.
Thanks for pointers!
EDIT:
I got it working like this:
<!--- getAsBinary --->
<cfhttp timeout="45"
throwonerror="no"
url="#variables.testFilePath#"
method="get"
useragent="..."
getasbinary="yes"
result="objGet">
<!--- validate --->
<cfif len(variables.testFilePath) EQ 0>
<cfset variables.errorCount = variables.errorCount+1>
<cfset variables.failedLoads = "FILE NOT FOUND" >
<cfelse>
<cfif len(objGet.Filecontent) EQ 0>
<cfset variables.errorCount = variables.errorCount+1>
<cfset variables.failedLoads = "COULD NOT LOAD IMAGE">
<cfelseif NOT listfindnocase(variables.allow, variables.fileExt) >
<cfset variables.errorCount = variables.errorCount+1>
<cfset variables.failedLoads = "WRONG FILE TYPE">
<cfelse>
<cftry>
<cfscript>
objImage = ImageNew(objGet.FileContent);
ImageSetAntialiasing(objImage,"on");
<!--- resize/crop --->
variables.keyName = Session.loginid & "_S_";
</cfscript>
<!--- convert modified image back to binary --->
<cfset variables.filekey = toBase64( objImage )>
<!--- pass to s3.cfc --->
<cfset result = s3.putObject(variables.bucketName, variables.filekey, variables.contentType, variables.httptimeout, variables.cacheControl, variables.cacheDays, variables.acl, variables.storageClass, variables.keyName, variables.imageSrc, "true" )>
<cfcatch>
<cfset variables.errorCount = variables.errorCount+1>
<cfset variables.failedLoads = "NO IMAGE">
</cfcatch>
</cftry>
</cfif>
I need to re-convert the cropped image to binary, because the s3.putobject will otherwise do another cffile action="readBinary" and breaks on trying to construct the image file path (the image is still in temp) right here:
<cffile action="readBinary" file="#arguments.uploadDir##arguments.fileKey#" variable="binaryFileData">
While I can get the temporary file path using this trick and set uploadDir it doesn't help, because CF docs say the path must be either an absolute path starting with drive letter or slash, otherwise the www-root temp directory will be taken.
In my case the temp www-root directory was on C:/ while the temp file CFFile-Servlet was on E:/ and a relative path did not work either (file not found). So as I found no way to re-read the image from the s3.cfc, I'm now converting back to binary before calling S3.cfc. I pass another parameter (1/0) telling s3.cfc, that I'm already sending the binary image and there is no need to re-read it.
Like so:
<!--- if encoded is true, filekey already is the encoded image --->
<cfif arguments.encoded EQ "true">
<!--- already sending binary image --->
<cfset binaryFileData = arguments.fileKey>
<cfelse>
<!--- Default --->
<cffile action="readBinary" file="#arguments.uploadDir##arguments.fileKey#" variable="binaryFileData">
</cfif>
I'm not sure if this is the smartest way performance wise, but it seems to work pretty smooth. Comments are welcome!
I guess you could use path and file attributes instead of result. Generate some temporary path using GetTempDirectory() + CreateUUID(), fetch and then drop it. Plus it may be a bit more memory-efficient thatn fetching content to the variable, then writing to the intermediate file.
Cfhttp result stores the data in a memory variable.
ImageNew creates a 'ColdFusion' image meaning it's resident in memory only also. You'd have to save it to make it a physical file to send either in cfhttp or imagewrite, etc.
Without saving it to a physical file you must use cffile action = "writetobrowser" to send it to a browser but that ends up saving it in a temp location for the browser to access but wouldn't do you much good here I don't think.
http://livedocs.adobe.com/coldfusion/8/htmldocs/functions_h-im_34.html
http://livedocs.adobe.com/coldfusion/8/htmldocs/Images_19.html
AmazonS3Client has a putObject method that takes an InputStream, so you can wrap the binary data in a ByteArrayInputStream and pass it in without worrying about the backing file.
Something like this:
<cfhttp method="get" url="#local.url#" result="local.httpResult" getAsBinary="true"/>
<cfif local.httpResult.statusCode eq "200 OK">
<cfset var fileInputStream = createObject("java", "java.io.ByteArrayInputStream" ).init(local.httpResult.fileContent) />
<cfset var objectMetadata = createObject("java", "com.amazonaws.services.s3.model.ObjectMetadata" ).init() />
<cfset application.S3UTIL.s3Client.putObject("some.bucket", "folder/file.ext", local.fileInputStream, local.objectMetadata)/>
</cfif>

Uploading Multiple Files

I have a form where users are to upload at least three documents and up to seven. In its current state, I have all the error checking and validation functioning. What I want to happen is for the visitor's forms to get renamed a specified name once they are uploaded and placed into a specified directory. My code is here: http://pastebin.com/V5ThWe7M
I believe the issue occurs around line 456. I believe I need to have the file name stored in a variable then use the variable to process the renaming function. The first file gets uploaded but not the second as they are assigned the same names. I'm trying to figure out how to use the variables that store the individual file names and then use that variable to rename the file. I'd like to have another set of eyes check it out for me and point me in the right direction.
Thank you
As already mentioned, the CFFILE structure is overwritten each time you upload. So any values you wish to preserve, you must save to another variable. But since you are already saving the full file names to a variable, you could easily extract their extensions using list functions. For example:
<cfset nomExt = listLast(clientNominationLetter, ".")>
A few other observations
Consider a more unique naming scheme for your folders than "/firstName_lastName/". Otherwise, you may end up overwriting someone's files if you receive multiple submissions under the same name, like two different "John Smith's".
The cffile values are separated into two categories: cffile.serverXX and cffile.clientXX (ie user system). They are not interchangeable. So be sure you are using the correct variables and be consistent.
FILE is deprecated. Use CFFILE or the result attribute instead.
EDIT: Adding some new code into the mix... :)
Here's some functions, just slap these in the top of your page...
<cffunction name="uploadFile">
<cfargument name="formField" hint="Form field name that holds the file to be uploaded" required="yes">
<cfargument name="renameTo" hint="What to rename the file, ex: 01_nominationLetter" required="yes">
<cfargument name="uploadErrorMessage" required="no" default="Error uploading file"/>
<cfargument name="allowedExtensions" required="no" default="doc,docx,pdf,txt,rt">
<cfargument name="extensionErrorMessage" required="no" default="Only doc, docx, pdf, txt, and rtf file formats are accepted">
<cfset var dir = expandPath("./nominationUploads/#trim(form.fname)#_#trim(form.lname)#/")>
<cfparam name="request.filesUploaded" default="#arrayNew(1)#">
<cftry>
<cffile action="upload" filefield="#arguments.formField#" nameconflict="makeunique" destination="#dir#">
<cfcatch type="any"><cfset ArrayAppend(arrErrors, arguments.uploadErrorMessage )></cfcatch>
</cftry>
<cfif not listFindNoCase(arguments.allowedExtensions, cffile.ServerFileExt)>
<cfset ArrayAppend(arrErrors, arguments.extensionErrorMessage )>
</cfif>
<cffile action="rename" file="#dir##cffile.serverFile#" destination="#dir##renameTo#.#cffile.ServerFileExt#">
<cfset ArrayAppend(request.filesUploaded, dir & arguments.renameTo & "." & cffile.ServerFileExt )>
</cffunction>
<cffunction name="removeFilesOnError">
<cfloop from="1" to="#arrayLen(request.filesUploaded)#" index="i">
<cftry><cffile action="delete" file="#request.filesUploaded[i]#"/><cfcatch type="any"></cfcatch></cftry>
</cfloop>
</cffunction>
Then in your existing validation script, get rid of all the file stuff, instead you'll only need to use those functions above, something like this :
<cfset uploadFile('myFileField', 'renameToThis' )>
<cfset uploadFile('myFileField2', 'renameToThat' , 'My custom upload error!')>
<cfset uploadFile('anotherFile', 'differentName', 'Another custom upload msg!', 'doc,docx', 'This one only lets you upload word docs!')>
<cfif arrayLen( arrErrors ) >
<cfset removeFilesOnError()>
</cfif>
I don't have time to test the above, but I believe it to be frighteningly close. If you encounter an issue, let me know and I'll help you debug it out. :)

How can I retrieve a (quasi) Array from a URL string?

I would like to achieve something I can easily do in .net.
What I would like to do is pass multiple URL parameters of the same name to build an array of those values.
In other words, I would like to take a URL string like so:
http://www.example.com/Test.cfc?method=myArrayTest&foo=1&foo=2&foo=3
And build an array from the URL parameter "foo".
In .net / C# I can do something like this:
[WebMethod]
myArrayTest(string[] foo)
And that will build a string array from the variable "foo".
What I have done so far is something like this:
<cffunction name="myArrayTest" access="remote" returntype="string">
<cfargument name="foo" type="string" required="yes">
This would output:
1,2,3
I'm not thrilled with that because it's just a comma separated string and I'm afraid that there may be commas passed in the URL (encoded of course) and then if I try to loop over the commas it may be misinterpreted as a separate param.
So, I'm stumped on how to achieve this.
Any ideas??
Thanks in advance!!
Edit: Sergii's method is more versatile. But if you are parsing the current url, and do not need to modify the resulting array, another option is using getPageContext() to extract the parameter from the underlying request. Just be aware of the two quirks noted below.
<!--- note: duplicate forces the map to be case-INsensitive --->
<cfset params = duplicate(getPageContext().getRequest().getParameterMap())>
<cfset quasiArray = []>
<cfif structKeyExists(params, "foo")>
<!--- note: this is not a *true* CF array --->
<!--- you can do most things with it, but you cannot append data to it --->
<cfset quasiArray = params["foo"]>
</cfif>
<cfdump var="#quasiArray#">
Well, if you're OK with parsing the URL, following "raw" method may work for you:
<cffunction name="myArrayTest" access="remote" output="false">
<cfset var local = {} />
<!--- parse raw query --->
<cfset local.args = ListToArray(cgi.QUERY_STRING, "&") />
<!--- grab only foo's values --->
<cfset local.foo = [] />
<cfloop array="#local.args#" index="local.a">
<cfif Left(local.a, 3) EQ "foo">
<cfset ArrayAppend(local.foo, ListLast(local.a, "=")) />
</cfif>
</cfloop>
<cfreturn SerializeJSON(local.foo) />
</cffunction>
I've tested it with this query: ?method=myArrayTest&foo=1&foo=2&foo=3,3, looks to work as expected.
Bonus. Railo's top tip: if you format the query as follows, this array will be created automatically in URL scope ?method=myArrayTest&foo[]=1&foo[]=2&foo[]=3,3.
listToArray( arguments.foo ) should give you what you want.