How to download password protected files from Edge or Chrome - coldfusion

How can I download password protected files when downloading the files from Edge or Chrome. Currently, I have code that works for files that are not password protected.
The following is the code I am using:
<cfif structKeyExists(URL, 'method') and URL.method eq 'download'>
<cfset file_name = URL.name />
<cfset path = URL.path />
<!--- The following if statements determine file type. --->
<cfif findNoCase('.doc', file_name) or findNoCase('.odt', file_name) or findNoCase('.rtf', file_name) >
<cfset file_type = 'application/msword' >
<cfelseif findNoCase('.pdf', file_name) >
<cfset file_type = 'application/pdf' >
<cfelseif findNoCase('.xls', file_name) or findNoCase('.xlt', file_name) or findNoCase('.csv', file_name)>
<cfset file_type = 'application/vnd.ms-excel' >
<cfelseif findNoCase('.tif', file_name) >
<cfset file_type = 'image' >
<cfelseif findNoCase('.jpg', file_name) >
<cfset file_type = 'image/jpeg' >
<cfelseif findNoCase('.url', file_name) or findNoCase('.lnk', file_name) >
<cfset file_type = 'text/uri-list' >
</cfif>
<!--- The following if checks which header to use --->
<cfif findNoCase('.msg', file_name) or findNoCase('.mdb', file_name) or findNoCase('.wmv', file_name) or findNoCase('.db', file_name)>
<!--- The following statements creates a downloadable link of the file. This is done by using cfheader and cfcontent --->
<cfheader name="Content-Disposition" value="attachment; filename=#file_name#">
<cfelse>
<!--- The following statements creates a downloadable link of the file. This is done by using cfheader and cfcontent --->
<cfheader name="Content-Disposition" value="inline; filename=#file_name#">
</cfif>
<!---Check if file type is available. If available, show type. --->
<cfif isDefined("file_type") >
<cfcontent type="#file_type#" file="#path#\#file_name#">
<!--- If file type is not found, display file anyways. --->
<cfelse>
<cfcontent file="#path#\#file_name#">
</cfif>
<cfabort>
</cfif>

Related

Unable to resize images in ColdFusion 8, file becomes hidden

My code takes an uploaded file, resizes it and attempts to save it to another file, from a temp folder to destination folder. I've tried all variations of sample code found online. When the cfimage tag goes to save the resized image, the file disappears from the original location. Then this error is thrown:
d:\temp\imageUploads\HeadshotsampleRDB.jpg (The system cannot find the file specified)
If I change the destination to the final location, the original file reappears!
<cffile
accept="image/gif, image/jpeg, image/jpg, image/pjpeg, image/png, image/xpng, image/x-png"
action="upload"
nameconflict="makeunique"
filefield="newphoto"
destination="d:\temp\imageUploads">
<cfset destFileName = session.Lastname & "_" & CreateUUID() & "." & cffile.serverFileExt>
<cfimage action="info" source="d:\temp\imageUploads\#cffile.serverfile#" structname="info">
<cfif info.width GT 100 OR info.height GT 150>
<cfif info.width GT 100>
<cfset lawyerImage=ImageNew("d:\temp\imageUploads\#cffile.serverfile#")>
<cfset ImageResize(lawyerImage,"100","","highestPerformance",1)>
<cfimage action="write" source="#lawyerImage#" destination="d:\temp\imageUploads\#cffile.serverfile#" overwrite="yes">
<cfelse>
<cfset lawyerImage=ImageNew("d:\temp\imageUploads\#cffile.serverfile#")>
<cfset ImageResize(lawyerImage,"","150","highestPerformance",1)>
<cfimage action="write" source="#lawyerImage#" destination="d:\temp\imageUploads\#cffile.serverfile#" overwrite="yes">
</cfif>
</cfif>
<cffile action="rename"
source="d:\temp\imageUploads\#cffile.serverfile#"
destination="#imgsPath#\photos\#destFileName#">
I could swear I have hot fix 4 installed, I see the file, but the CF Admin only shows us at hot fix level 2.

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" >

CFLOG does not write to log file

I want to write some information to a log file. I tried adding this code to my file:
<cfparam name="BillingAddressOne" default="#session.userinfo.Address1#">
<cfparam name="BillingAddressTwo" default="#session.userinfo.Address2#">
<cfparam name="BillingCity" default="#session.userinfo.city#">
<cfparam name="BillingState" default="#session.userinfo.state#">
<cfparam name="BillingZipCode" default="#session.userinfo.zip#">
<cfparam name="BillingCountry" default="US">
<cfparam name="BillingPhone" default="#session.userinfo.phone#">
<cfparam name="BillingFax" default="#session.userinfo.fax#">
<cfparam name="BillingEmail" default="#session.userinfo.email#">
<cfparam name="CardType" default="#sessioninfo.CreditCardType#">
<cfparam name="form['Credit Card Number']" default="#sessioninfo.CreditCardNumber#">
<cfparam name="expmonth" default="#sessioninfo.CreditCardExpMonth#">
<cfparam name="expyear" default="#sessioninfo.CreditCardExpYear#">
<cfparam name="CCverificationCode" default="#sessioninfo.CCverificationCode#">
<cfparam name="CreditCardName" default="#sessioninfo.CreditCardName#">
<cflog file="info.log" application="no"
text="#BillingAddressOne# #BillingAddressTwo# #BillingCity# #BillingState# #BillingZipCode# #BillingCountry# #BillingPhone# #BillingEmail# #pmtmethod# #CardType# #form['Credit Card Number']# #expmonth# #expyear# #CCverificationCode# #CreditCardName#">
I tried inputting something but nothing shows in file info.log
According to the docs the file attribute should only contain the file name, not the extension.
Also,
The file must be located in the default log directory. You cannot
specify a directory path. If the file does not exist, it is created
automatically, with the extension .log.
This will most likely be [coldfusion install folder]/logs
Try
<cflog file="info"
application="no"
text="#BillingAddressOne#
#BillingAddressTwo#
#BillingCity#
#BillingState#
#BillingZipCode#
#BillingCountry#
#BillingPhone#
#BillingEmail#
#pmtmethod#
#CardType#
#form['Credit Card Number']#
#expmonth#
#expyear#
#CCverificationCode#
#CreditCardName#">