Saving content to a .DOC file using ColdFusion - coldfusion

I'm not having any issues writing the content to the .doc file. The problem I'm having is getting the file to NOT download to the user's browser automatically after creation. I just want to have the .doc file created in the background, then the user can download the file from a web page at anytime. Here's the code I'm working with:
<cfheader name="Content-disposition" value="filename=Quote_#arguments.QuoteNumber#_#arguments.Revision#.doc">
<cfcontent type="application/msword">
<cfoutput>#WordDoc#</cfoutput>
<cffile action="copy" source="#application.AbsPath#\media\quotes\BlankQuote.doc" destination="#application.AbsPath#\media\quotes\Quote_#arguments.QuoteNumber#_#arguments.Revision#.doc" />
<cffile action="write" file="#application.AbsPath#\media\quotes\Quote_#arguments.QuoteNumber#_#arguments.Revision#.doc" output="#WordDoc#" />

You're problem is the <cfheader> tag... that's what is triggering the doc to open.
I would do something like this instead.
<cfsavecontent variable="whatever">
<cfoutput>#WordDoc#</cfoutput>
</cfsavecontent>
<cffile action="write" file="#application.AbsPath#\media\quotes\Quote_#arguments.QuoteNumber#_#arguments.Revision#.doc" output="#whatever#" />

Related

coldfusion - prevent direct access to files created by user that are meant to be downloaded

When a user logs into our system, it's possible for the user to create a CSV report. This report can then be downloaded by the user. Directory structure is like this:
siteroot.com/user/
siteroot.com/user/login/
siteroot.com/user/login/downloads/[file].csv
The /login/ directory can only be accessed via a logged in user. However someone - if they knew the URL - could go directly to the URL of the CSV file and grab the file:
siteroot.com/user/login/downloads/myfile.csv
Is there any better (simpler) solution than moving them out of the root and then using a cfdirectory / cfheader / cfcontent to pull the CSV file out of a non-IIS directory and give it to the user?
Don't put those files in a web accessible folder.
If you're creating a data export that does not need to be kept around, create the file in a temp folder (also not a publicly accessible folder) and use <cfcontent> to push the file to the browser, which will present the user with a download dialog. You'll delete the file from the server as part of this process, so you'll run this process every time someone requests the export.
This keeps your server clean and keeps the file behind a login. Here's a gist I created to stub out this process. It uses <cfspreadsheet> to accomplish the export. Here's the core of the code:
<cfspreadsheet action="write" filename="#config.full_temp_name#" query="config.q" />
<cfspreadsheet action="read" src="#config.full_temp_name#" name="local.xls" />
<cffile action="delete" file="#config.full_temp_name#" />
<cfif len(arguments.fileName) GT 0>
<cfheader name="content-disposition" value="attachment; filename=#arguments.fileName#.#config.extension#" />
<cfelse>
<cfheader name="content-disposition" value="attachment; filename=#config.temp_name#.#config.extension#" />
</cfif>
<cfcontent type="application/msexcel" variable="#spreadsheetReadBinary(local.xls)#" reset="true" />

Using cfimage to display a file that doesn't have an extension

curious one this.
I'm working on a process that generates PDF files, combining data from various sources. The last piece of this process I need to complete is merging in image files.
This is actually fairly straightforward but the problem I have is the image files aren't stored with file extensions. Locally, I can change the filename, but in production this isn't an option.
So because a filename looks like : B71637CB-A49C-0653-EF813918736BDEB7
This will not work:
<cfimage action="writeTobrowser" source="#FilePath#>
Same with
<img src="#FilePath#">.
So, any ideas on how I can work around this? Here's the code in context:
<cfdocument format="PDF" name="report" filename="#fileToDownloadimage#" overwrite="yes">
<cfdocumentsection>
<cfimage action="writeTobrowser" source="#FilePath#.jpg">
</cfdocumentsection>
</cfdocument>
So here's what ended up working:
<cfdocument format="PDF" name="report" filename="#fileToDownloadimage#" overwrite="yes">
<cfdocumentsection>
<cfset fileObject = fileReadBinary('#FilePath#') />
<cfset imageObject = imageNew(fileObject) />
<cfimage action="writeTobrowser" source="#imageObject#">
</cfdocumentsection>
</cfdocument>
Alex's answer got me down the right path so I'm perfectly happy to leave the kudos in place, cos I wasn't getting anywhere near this!
If you need to embed the images into the PDF document, try HTML's inline image capabilities:
<cfset fileLocation = "/path/to/images/B71637CB-A49C-0653-EF813918736BDEB7">
<cfset imageContent = fileReadBinary(fileLocation)>
<cfset imageContentAsBase64 = toBase64(imageContent)>
<cfoutput>
<img src="data:image/jpeg;base64, #imageContentAsBase64#" />
</cfoutput>
You can try creating a cfm page that outputs your content using cfcontent as in:
<cfcontent type="image/jpg" file="path/#fielpath#">
Then you would you include THAT cfm page as the source for your image as in
<img src="myFancyImageOuputer.cfm?image=#filepath#">
This should work but it may require some trial and error. :)
Ray has some additional tips here:
http://www.raymondcamden.com/2007/09/14/Serving-up-CFIMages-via-Image-Tags-and-a-NonCF-Friday-contest

how to save xml in user's computer in coldfusion?

I am invoking a ColdFusion webservice through cfinvoke
<cfinvoke
method="getUsers"
returnvariable="rawXMLUserList"
webservice="http://www.xyz.com/getusers.cfc?wsdl"
>
<cfinvokeargument name="userid" value="123">
</cfinvoke>
And I am storing XML returnvariable into userList variable
<cfset userList = XmlParse(rawXMLUserLis)><br/>
how to save abc.xml on user's computer, using cffile it is saving on server's computer, i have to save it on user's computer who invokes this "getUsers" method.
Thanks
Kishor
When you run XMLParse(), you're turning it into a CF XML Document Object. You need to use ColdFusion's toString(xmlObject) function when outputting it.
<cfheader name="content-disposition" value="attachment;filename=abc.xml">
<cfcontent type="application/xml;charset=utf-8" reset="true">
<cfoutput>#toString(userList)#</cfoutput>
Another way is to write the file to a web directory (cffile action=write) and then cflocation the user to the file.
You need to tell the user's browser to download the file, not display it. Generally with something like this (not tested):
<cfheader name="content-disposition" value="attachment;filename=abc.xml">
<cfcontent type="application/xml;charset=utf-8" reset="true">
<cfoutput>#userList#</cfoutput>
cfheader generates a custom HTTP header.
cfcontent sets the MIME content encoding header for the page.

Coldfusion - cannot access templ folder when creating thumb

I am getting a problem with the following line:
<cfimage action="read" name="myImage" source="#ExpandPath("../../banner/#upload.clientfile#")#" />
I suspect it is because I am using a shared host (CF9) and do not have access to the folder. The error I get is "unable to create temporary file". My temp directory is home/kloxo/temp/wwwroot-tmp. Can I specify another temp folder or do I have to get my hosting company to sort this?
<cfapplication sessionmanagement="true">
<cfoutput>#GetTempDirectory()#</cfoutput>
<cfif IsDefined ("FORM")>
<cfif structKeyExists(form, "uploadfile")>
<cfset destination = expandPath("../../banner")>
<cfif not directoryExists(destination)>
<cfdirectory action="create" directory="#destination#">
</cfif>
<cffile action="upload" filefield="uploadfile" destination="#destination#" nameConflict="makeUnique" result="upload">
<cfdump var="#upload.clientfile#">
<cfimage action="read" name="myImage" source="#ExpandPath("../../banner/#upload.clientfile#")#" />
</cfif>
It appears to be a configuration issue. Contact your host. Here is a page with more information:
http://forums.adobe.com/message/3060530

How to get file extension when renaming file with cffile

I'm using a cffile tag to upload my file and resave it with a new name. My issue is that the file could be a few different formats and I don't know how to detect the file extension. I'm using the code below:
<cfset ui = createUUID()>
<cffile
action="upload"
accept="video/x-flv, video/mp4, video/x-msvideo"
destination="e:\www2\uploads\#ui#.#cffile.ServerFileExt#"
nameconflict="makeunique"
filefield="form.file"
>
It's telling me that cffile is undefined.
I recommend uploading first, then renaming:
<cfset ui = createUUID()>
<cffile
action="upload"
accept="video/x-flv, video/mp4, video/x-msvideo"
destination="e:\www2\uploads\"
nameconflict="makeunique"
filefield="form.file"
/>
<cffile
action="rename"
source="e:\www2\uploads\#cffile.serverFileName#"
destination="e:\www2\uploads\#ui#.#cffile.serverFileExt#"
/>
I found this awesome function created by Ryan Stille
It should do everything you need
I used it to get the extension then I just created a file name with a UUID
<cffile action="upload" destination="file://upload_#createUUID()#.#fileExt#" nameconflict="makeunique" result="#formField#">