Link to home
Start Free TrialLog in
Avatar of Shane_Capper
Shane_Capper

asked on

Cold fusion code to download files (eg Word/Excel) from an FTP/WWW Site, and offer it for download.

First of all, is it possible for cold fusion to retrieve a file from an ftp/www site and then prompt the user to save/view that file from the brower? If so, can you provide brief code as an example. I have tried using the cfttp, cfftp and cfcontent tags to a limited degree of success. I can retrieve the file from the site, and also prompt the user to save the file, but it is usually garbled (looks like it has been converted into a string).

The reason behind this is to hide the original source of the files, as the source is protected and I do not wish the credentials to be hard coded into the link.

Thanks,
Shane.
Avatar of pinaldave
pinaldave
Flag of India image

hello,
You have two Questions
1) FTP... it works and I have done it many times... the code is simple and the best one is on the web site of macromedia.
http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-p39.htm#wp1800554
There are two cat for ftp
i) you need to open connection.
ii) you need to use hte file operation of ftp..
regarding the code, if i will give you any code there will be no different then the code on web site....
2) second content is of file content...
let me show you small code for excel and you can do the same for word....
<cfquery datasource="MyDataSourceHere" name="qryAccessData">
     SELECT Field1, Field2, FieldN
          FROM AccessTable
</cfquery>
<cfheader name="content-disposition" value="inline;filename=AccessToExcelDump.xls">
<cfcontent type="application/msexcel">
<table border="1">
     <tr>
          <th>Field1</th>
          <th>Field2</th>
          <th>FieldN</th>
     </tr><cfoutput query="qryAccessData">
     <tr>
          <td>#qryAccessData.Field1#</td>
          <td>#qryAccessData.Field2#</td>
          <td>#qryAccessData.FieldN#</td>
     </tr></cfoutput>
</table>

you may not need the query and you can do it without that... also....
some things to read more about in the same line of ideas....
https://www.experts-exchange.com/questions/20701038/how-to-export-from-SQL-query-to-excel-no-'COM'-objects-please.html
https://www.experts-exchange.com/questions/20918372/How-to-export-access-table-to-Excel.html

If you have any problem regarding ftp or any code problem let us know.
Regards,
---Pinal
Avatar of Shane_Capper
Shane_Capper

ASKER

Thanks for your quick response, but I dont think you really answered my question. I have successfully used code like yours above, but not when dealing with binary data (like a word doc). The code I was playing around with is below. It obviously doesnt work, but I'm not sure what I am doing wrong.

------------------------------------------------------------------------------------
<cfheader name="content-disposition" value="inline;file=word.doc">
<cfcontent type="application/msword">
 
<cfhttp url="http://www.webserver.com/myworddocument.doc" username="xxxxxx" password="xxxxx" port="80" method="GET" getasbinary="yes">
<cfoutput>#cfhttp.FileContent#</cfoutput>
------------------------------------------------------------------------------------
Hello,
May be I have not answered your question...
I have not much used http...
though, I was wondering what is the error...or what is the output which you are not loooking for ...
Regards,
---Pinal.
There is no real error message. What happens is IE prompts me to download the test.cfm page, but then complains it cant find it. Stange hey.
just try this... for kicks... i am looking into this ... before I go to sleep ... it is pretty late here...
<meta http-equiv="Refresh" content="1;URL=download.cfm">

Just put it in the HEAD tags of your HTML document.  This will wait one second then try running download.cfm which has your CFHEADER and CFCONTENT tags.
Same thing happens. Thanks for your help, I appreciate this!
ASKER CERTIFIED SOLUTION
Avatar of pinaldave
pinaldave
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Pinal, I think I should be able to manage from here. You've given me a few options that I think will do the trick. You have the points.

Shane.
Ahh, I managed to get an error out of coldfusion which should point me in the right diredction.

Error: ByteArray objects cannot be converted to strings.  
For those interested, final working code below.

--------------------------------------------------
<cfsetting enableCFoutputOnly = "Yes">
<cfsilent>
<cfhttp url="http://www.webserver.com" username="myusername" password="mypassword" throwOnError="yes" port="80" method="GET" getasbinary="yes">
<cfheader name="content-disposition" value="attachment;filename=word.doc">
<cfcontent type="#cfhttp.mimetype#">
</cfsilent>
<cfscript>writeOutput(toString(cfhttp.filecontent));</cfscript>
--------------------------------------------------

Thanks Pinal! All credit to you.