Text File

Discussion in 'Programming' started by gigamike, Apr 12, 2007.

  1. #1
    Hi Guys,

    I hope you can help me, im new to coldfusion. Im planning to have a button and when i press the button, it will download a CSV file extracted from table. Any idea or sample.

    Thanks,

    Mike
     
    gigamike, Apr 12, 2007 IP
  2. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Hello GigaMike,

    We are real eager to help you out - but give it a try, and post some code. Let's start there - coldfusion is much like cooking - sure you can follow the instructions, but the most tasty and surprising of meals and done when cooking is looked at as an art form - a passion. So go head, get your hands dirty and give it a try - let's see where it goes!
     
    datropics, Apr 12, 2007 IP
  3. gigamike

    gigamike Active Member

    Messages:
    165
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Hi datropics,

    thanks for the inspiring words...ok here is what i did, upon the SUBMIT button is press, this code will be executed:

    <cfif isDefined("FORM.submit")>
    <cfset date=NOW()>
    <cfset sdate=DateFormat(date, "mmddyyyy")>
    <cfset stime=TimeFormat(date, "HHmmss")>
    <cfset datetime=sdate & stime>

    <!--- Create a Text File--->

    <!--- make some variables to store the dirctory and file name. --->
    <cfset f_dir = "C:\webfiles\www1.alphared.com\admin\textfiles\">

    <!--- I like to use the date and time for a file name, but you can name it anything you like by changing the value below. --->
    <cfset f_name = "#trim(datetime)#.csv">

    <!--- Lets make the file, and put the first row of Column headings in --->
    <cffile action="WRITE" file="#f_dir##f_name#"
    output="Email, Name, Comments, Date Inserted" addnewline="Yes">

    <!--- Now lets loop over the RecordSet and fill in the data --->
    <cfquery name="qry_mailing_list" datasource="ardatabase">
    SELECT *
    FROM mailing_list
    ORDER BY email, name
    </cfquery>
    <cfif qry_mailing_list.RecordCount GT 0>
    <cfloop query="qry_mailing_list">
    <cfset email = #trim(qry_mailing_list.email)#>
    <cfset name = #trim(qry_mailing_list.name)#>
    <cfset comments = #trim(qry_mailing_list.comments)#>
    <cfset date_inserted = #trim(qry_mailing_list.date_inserted)#>

    <cffile action="APPEND" file="#f_dir##f_name#" output="#email#, #name#, #comments#, #date_inserted#" addnewline="Yes">
    </cfloop>
    </cfif>

    <cfset msg="<a href='textfiles/#f_name#'>Here is the file</a>">
    </cfif>

    Notice that im storing/saving it to a directory, what i want to happen is that when i press the SUBMIT button, it will create a .CSV (currently its working) but instead of storing in a directory, it will be downloaded (Asking user to download). I already did this to php something like this:

    // Outputting CSV Report
    @header("Content-type: application/vnd.ms-excel");
    @header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
    // IE needs specific headers.
    if (strstr($_SERVER['HTTP_USER_AGENT'],"IE")) {
    @header('Content-Disposition: inline; filename="'.$datetime_today.".csv".'"');
    @header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    @header('Pragma: public');
    } else {
    @header('Content-Disposition: attachment; filename="'.$datetime_today.".csv".'"');
    @header('Pragma: no-cache');
    }
    echo $content;


    Hope you guys can help me or give me an idea.

    Thanks,

    Mike


     
    gigamike, Apr 13, 2007 IP
  4. IsRoss()

    IsRoss() Peon

    Messages:
    116
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi GigaMike -

    If you add this to the beginning of your template, it should give the user the option to either save the file or open it with excel:

    <cfheader name="Content-Disposition" value="inline; filename=mystuff.xls">
    <cfcontent type="application/vnd.ms-excel" file="c:\mystuff.xls">

    <!--- your code to create the csv data goes here --->
     
    IsRoss(), Apr 13, 2007 IP
  5. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #5
    IsRoss is correct gigaMike
     
    datropics, Apr 13, 2007 IP