Problems with CFZIP on ColdFusion 11 - coldfusion

My company recently migrated from Coldfusion 9 to Coldfusion 11. One problem I haven't been able to resolve from this migration is to do with the the way CFZIP works.
In the old server we had a place in our app where we could upload zip files with images and then Coldfusion would unzip them and place them on the server.
<!--- Upload the zip --->
<cffile action="UPLOAD" filefield="vchrZip"
destination="#app_sysfilepath_site#images\somefolder\zip\"
nameconflict="makeunique">
<!--- Create a directory for our images --->
<cfset folder = Mid(file.serverfile, 1, Len(file.serverfile) - 4)>
<cfdirectory action="create" directory="#app_sysfilepath_site#images\somefolder\#folder#">
<!--- Unzip the zip file and place the contents to the directory--->
<cfzip action="unzip"
file="#app_sysfilepath_site#images\somefolder\zip\#file.serverfile#"
destination="#app_sysfilepath_site#images\somefolder\#folder#"
storepath="false">
On coldfusion 9, this would upload the image files to the root of #app_sysfilepath_site#images\somefolder#folder#"
#app_sysfilepath_site#images\somefolder\#folder#"
----> Image.png
----> Image2.png
----> Image3.png
On Coldfusion 11, this is putting a subfolder with the same name as the zip file with the images inside of it.
#app_sysfilepath_site#images\somefolder\#folder#"
---->#folder#
----> Image.png
----> Image2.png
----> Image3.png
I tried changing the storepath to 'no', but that didn't work.

Related

ColdFusion list a particular type of files within folders?

I have a list of folders from a directory. All of the folders in this directory contain pdf files. There are about 20 folders. Is it possible to get the names of the pdf files, in all folders, without having to go through each directory (folder)?
<cfdirectory action="list"
directory="C:\wwwroot\WebServer\testing\uploads\all_folders"
recurse="false"
name="myList">
<cfdump var=#myList#>
Try using filter="*.pdf" attribute and type="file".
<cfdirectory
directory="C:\wwwroot\WebServer\testing\uploads\all_folders"
action="list"
recurse="yes"
type="file"
filter="*.pdf"
name="myList"
>
<cfdump var=#myList#>

Looping through cfquery for file move is failing

How do I loop through a query and pass one result at a time rather than all results?
I'm running a query and using <cfloop> to loop through the result and for every file name in the query, <cffile> needs to move that file from one folder to the next as follows:
cfquery name="qryGetFilesJustUploaded" datasource="#request.dsn#"> <!--- Limit to filed with pdf file type endings --->
SELECT fileupload
FROM [DevDBServer].[dbo].[upload_many]
WHERE (fileupload Like '%.pdf%') and (needs_compression = '1')
</cfquery>
cffunction name="MoveCompressReturnFile" access="public" returntype="void" description="Moves file to temp folder, compresses it then returns it to its original location">
<!---Lets Loop through and move all files references in query--->
<cfloop query="#qryGetFilesJustUploaded#">
<cffile action="move"
source="C:\inetpub\wwwroot\testingFolder\PDFCompression\pdf\#qryGetFilesJustUploaded.fileupload#"
destination="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin" >
<!--- Now lets compress it--->
<cfexecute name="C:\Program Files (x86)\neeviaPDF.com\PDFcompress\cmdLine\CLcompr.exe"
arguments="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\#qryGetFilesJustUploaded.fileupload# C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\#qryGetFilesJustUploaded.fileupload# -co -ci jpg -cq 10 -gi jpg -gq 10 -mi jbig2 -mq 1"
outputfile="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\output.txt"
timeout="250">
</cfexecute>
<!---Now Lets Return the file back to its original folder--->
<cffile action="move"
source="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\#qryGetFilesJustUploaded.fileupload#"
destination="C:\inetpub\wwwroot\testingFolder\PDFCompression\pdf" >
</cfloop>
</cffunction>
When it comes to the point of running the first cffile command, I get an error message that C:.... C:.... is not a valid source and what it looks like its doing is specifying multiple files at once rather than grab them one at a time until done grabbing all files referenced in the query. How do I fix this?
Update: Code is not successfully looping through the query results.
1. Tested the code using filename="document1.pdf" and it worked without issue passing that into the FindFilePath method and bringing back the file path and executing the remainder of the code. However when its replaced with the filename="qryGetFilesJustUploaded", each of the file names in the query doesn't seem to be getting passed in successfully so the file path of that file can be returned.
Did a dump of the query to verify my query works and the list of files is available.
Also verified the files exist and that there is no odd formatting in the file names.
You can try putting sleep(5000) after each file operation. This is because file operation take some time to complete. And it may be possible that while the first file operation is going on, the next file operation started. And thus in some cases you might encounter such kind of issues. Try this.
<cfloop query="#qryGetFilesJustUploaded#">
<cffile action="move" source="C:\inetpub\wwwroot\testingFolder\PDFCompression\pdf\#qryGetFilesJustUploaded.fileupload#"
destination="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin" >
<cfset sleep(5000)>
<!--- Now lets compress it--->
<cfexecute name="C:\Program Files (x86)\neeviaPDF.com\PDFcompress\cmdLine\CLcompr.exe"
arguments="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\#qryGetFilesJustUploaded.fileupload# C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\#qryGetFilesJustUploaded.fileupload# -co -ci jpg -cq 10 -gi jpg -gq 10 -mi jbig2 -mq 1"
outputfile="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\output.txt"
timeout="250">
</cfexecute>
<cfset sleep(5000)>
<!---Now Lets Return the file back to its original folder--->
<cffile action="move"
source="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\#qryGetFilesJustUploaded.fileupload#"
destination="C:\inetpub\wwwroot\testingFolder\PDFCompression\pdf" >
<cfset sleep(5000)>
</cfloop>
Resolved
When I set my query to the filename, I forgot to set it to "queryname.columnname" syntax.
Working Code:
<cffunction name="testFunc" access="public">
<!--Lets Get the file names from the DB table--->
<cfquery name="qryGetFilesJustUploaded" datasource="#request.dsn#">
SELECT fileupload
FROM [First_Title_Services_Dev].[dbo].[upload_many]
WHERE (fileupload Like '%.pdf%') and (needs_compression = '1')
</cfquery>
<cfset qryRecordCount = #qryGetFilesJustUploaded.RecordCount# >
<!--Set the methods and variables--->
<cfset fileSys = CreateObject('component','cfc.FileManagement')>
<cfloop query="qryGetFilesJustUploaded" startRow="1" endRow="#qryRecordCount#">
<cfset filename="#qryGetFilesJustUploaded.fileupload#" >
<cfset filePath = #fileSys.FindFilePath('#filename#','file')# >
<cffile action="move" source="#filePath#" destination="C:\compressionBin">
<cfset sleep(5000)>
<!---Compress the file now--->
<cfexecute name="C:\Program Files (x86)\neeviaPDF.com\PDFcompress\cmdLine\CLcompr.exe"
arguments="C:\compressionBin\#filename# C:\compressionBin\#filename# -co -ci jpg -cq 10 -gi jpg -gq 10 -mi jbig2 -mq 1"
outputfile="C:\inetpub\wwwroot\testingFolder\PDFCompression\bin\output.txt"
timeout="250">
</cfexecute>
<cfset sleep(5000)>
<!---Lets Return the file now--->
<!---Back to where you came from! :D--->
<cffile action="move"
source="C:\compressionBin\#filename#" destination="#filePath#" >
<cfset sleep(5000)>
</cfloop>
</cffunction>

Get a file from auto download url in coldfusion

I am trying to get a file from a auto download url using cfhttp.
I am using the following code:
<cfhttp method="get" url="http://www.example.com/getfile" path="E:/" file="abc.csv">
In this case I have specified the file type as CSV so I am able to get the file but file type can change.
I tried CFHTTP.MIMETYPE to get the file type and use like this:
<cfhttp method="get" url="http://www.example.com/getfile">
<cffile action="write" file="E:/abc.#listLast(cfhttp.MIMETYPE,'/')#" output="#cfhttp.FileContent#">
And this is working for CSV and XML files. But I want it to work to Excel files also.
Please help.
Thanks in advance.
<cfhttp method="get" url="http://www.example.com/getfile">
<cfset fileName = listlast(cfhttp["responseHeader"]["content-disposition"],";=")>
<cffile action="write" file="E:/abc.#fileName#" output="#cfhttp.FileContent#">
As "Regular Jo" mentioned, you need to add getAsBinary="Auto" to the cfhttp to get this working. Thank you to Deepak Kumar Padhy for the inital answer that pointed me in the right direction.
<cfhttp method="get" url="http://www.example.com/getfile" getAsBinary="Auto" result="cfhttp">
<cfdump var="#cfhttp#">
<!--- Returns the file name with double quotes, ex. '"Users.zip"' --->
<cfset fileNameWithQuotes = listlast(cfhttp["responseHeader"]["content-disposition"],";=")>
<!--- Remove the '"' in the file name so it is Users.zip --->
<cfset fileName = REPLACENOCASE(fileNameWithQuotes,'"','', 'ALL')>
<!--- Write the zip to the server location --->
<cffile action="write" file="E:/abc/#fileName#" output="#cfhttp.FileContent#">
<!--- if zip file, unzip the file to get the csv --->
<cfzip file="E:/abc/#fileName#" action="unzip" filter="*.csv" destination="E:/abc/" />

How can I download all attachments from a folder in ColdFusion

How to download all files from a folder in ColdFusion? I have a folder named "abc" and there are 5 different files in it. So I want to download all 5 files when I click on a hyperlink.
Currently, this is the code I am trying. It is downloading a selected file, but how do I download all files in it?
<cfheader name="Content-disposition" value='attachment; filename="database_error.png"'>
Check this code
<cfset fileName = createUUID() />
<cfif DirectoryExists("D:/tempDir")>
<cfdirectory action = "list" directory = "D:/tempDir" name = "getAllFiles" type="file" />
<cfzip action="zip" file="D:/zipfilefolder/#fileName#.zip" overwrite="yes" >
<cfloop query="getAllFiles">
<cfzipparam source="D:/tempDir/#getAllFiles.name#">
</cfloop>
</cfzip>
</cfif>
<cfheader name = "Content-disposition" value = 'attachment; filename="D:/zipfilefolder/#fileName#.zip"'>
<cfcontent deleteFile="true" file="D:/zipfilefolder/#fileName#.zip" type="application/x-zip-compressed" >

Generate multiple files, add them to a zip and then send the zip as a file download.

I have a cfm page that generates multiple spreadsheet objects, right now if i try to send the files for download, only the last generated spreadsheet file is send as a download.
Is there a way to write these files to a zip and send the zip file as a download?
This is my existing logic:
for(VARIABLES.fileNumber = 0; VARIABLES.fileNumber < VARIABLES.maxFiles; VARIABLES.fileNumber = VARIABLES.fileNumber + 1)
{
/* code to create Spreadsheet here */
VARIABLES.context = getPageContext();
VARIABLES.context.setFlushOutput(true);
VARIABLES.response = VARIABLES.context.getResponse().getResponse();
VARIABLES.response.reset();
VARIABLES.response.setContentType("application/msexcel");
VARIABLES.response.setContentLength(len(SpreadsheetReadBinary(VARIABLES.suppItemSpreadSheetObj)));
VARIABLES.response.setHeader("Content-Disposition","attachment;filename=MyComplianceStatusFile#VARIABLES.fileNumber#Of#VARIABLES.maxFiles#.xls");
VARIABLES.out = response.getOutputStream();
VARIABLES.out.write(SpreadsheetReadBinary(VARIABLES.suppItemSpreadSheetObj));
VARIABLES.out.flush();
VARIABLES.out.close();
}
Now , this way I get only the last spreadsheet that was generated. Is there a way to get all the spreadsheets that are generated may be one by one or may be in a zip ?
Yes there is: <cfzip>. There's a usage example in the docs, but I'll repro here so I don't get slapped for basically saying "RTFM":
<!--- This example shows how to zip the directory "c:\temp" into the ZIP file "e:\work\abc.zip". --->
<cfzip file="e:\work\abc.zip" source="c:\temp">
<!--- This example shows how to zip all the class files in a directory and add a subdirectory named "classes" to the JAR file entry name. --->
<cfzip file="e:\work\util.jar" action="zip" source="c:\src\util\" prefix="classes" filter="*.class">
<!---This example shows how to zip all of the log files in the ColdFusion directory and create a subdirectory called exception where zipped files are archived.
<cfzip file="c:\zipTest\log2.zip" action="zip" source="c:\ColdFusion\" prefix="exception" filter="*.log">
<!--- This example shows how to overwrite all of the content of a ZIP file with the entries specified in the source. --->
<cfzip file="c:\currentApp.zip" source="c:\myApp\work" overwrite="yes">