Saving Binnary Flies ColdFusion Help

Just as you can save plain text and HI’ML downloaded from a Web server to files on the ColdFusion server, so can you save binary files. When you do this, the MIME type of the file is stored in the CFHTTP. MimeType variable. For instance, if you download a ZIP file with the following tag:

<CFHITP METHOD-GET
URl·’http://www.landegg.edu/test.zip
PATH-·c:\html·
FILE··landegg.zip

Converting a Text File into a Query

Another use of the GETmethod is to retrieve a delimited text file of the described earlier intlilifchapter and convert it into a.query.

Let’s start with a sample delimited text file:

This file uses commas as the delimiters and contains three records with three fields or columns (fi rstname, 1astname, gender).

If this file is stored on a Web server at local host/file. txt, then you can download this file, as you have learned before, by using the following:

However, all this does is place the entire contents of the file, unprocessed in any useful way, into the CFHlTP. Fi 1eContent variable. To convert this file into a query, you will use the NAMED,ELIMITERa, nd TEXTQUAlIFIER attributes:

<CFHTTP METHOO-GET
URL-‘http://localhost/file.txt’
NAME-‘people ,
DELIMITER-
TEXTQUALIFIER-“>

Here you specify the following:
• The resulting query is called. people.
• The delimiter is a comma.
• No text qualifier is specified. Therefore, the delimiter cannot appear within a field.

Knowing this, you can proceed. to use the query in your template. For instance, the following template would download the preceding file and display it in a table:

If you need to use a text qualifier, the process changes slightly. For instance, if you add a fourth record that contains a comma in the fi rstname field, you now need to use a text qualifier. Two steps are required: First, each field in the text file needs to be-enclosed in the qualifier. Second, you need to specify the qualifier in the TEXTQUALIFIER attribute. The result is a delimited text file like this:

Notice that you have used a double quote as a qualifier and that in the TEXTQUALIFIER attribute you have had to escape the double quote.

Posting Data to a Server

The final method you are going to look at is how to use the POSTmethod to send data to a server for processing. Using the POSTmethod, you can send the following types of da ta to the server

URL The server will receive the data as if it were appended to the URL using the standard variable  data2 &variable2-data2 syntax.
Formfield The server will receive the data as if it were entered in a named form field by a user and then submitted using the POSTmethod of the. FORMtag.
Cookie The server will receive the data as if the browser had returned cookie data to the server for the requested document. CGI The server will receive the data and make it available as a standard CGI The environment variable. This can be used to set CGI environment variables such as USERID.
File. The server will receive the data as an uploaded file that can then be saved to disk, for instance with the CFFILE tag.

To upload these types of data to the server, you need to use the CFHTIPPARAMtag

Using the CFHrrPPARAM Tag

The CFHTIPPARAMtag is used to specify name-value pairs for any of the data types outlined in the preceding list. It takes on four attributes:

NAME The variable name associated with the data being sent to the server iYPE The type of data being sent to the server based on the preceding list
VALUE The data being sent to the server (with TYPE-‘ File’ , this attribute is Clot required)
FILE Specifies the file to upload when using TYPE-‘ Fi 1e’
Consider the following five CFHTIPPARAMtags

These five tags upload the following data, respectively:
• A URLvariable named URLvar with the value URLdata
• A FormField named Formvar with the value Formdata
• A Cookie named Cooki evar with the value Cooki edata
• A CGI variable named CGlvar with the data CGldata
• A File field named Fi 1evar containing the contents of the file c: \ temp\ foo. txt

To use these CFHTIPPARAMtags with CFHTIP to upload the data to a URL, you need to use the following construct:

Notice that you use METHOD=’POST” in the CFHTIP tag and that the CFHTIPPARAMtacs are enclosed within opening and closing CFHTTPtags (the closing tag \, asn’tnp”Jt’o when you used METHOD'”GET’).

Given this, if you submit this data to another ColdFusion temPlate, then the first four pieces of data being sent could be accessed in that template as:

• URL.URLvar
• Form. Formvar
• Cookie.Cookievar
• CGI.<!Glva

Posted on November 14, 2015 in Using the HTTP Protocol.

Share the Story

Back to Top
Share This