ColdFusion, .Net (dotnet) and byte arrays

Problem: Using ColdFusion's integration with .Net you can use 'System.IO.File' to read in a file and get back the byte array of the file, however it is not a a byte array that you'd expect i.e. a binary object. It is actually translated to an CF Array. This makes it difficult to use CF to deal with .Net byte arrays.

Solution: However you can use .Net's 'System.Convert' to convert it to a Base64 string, something CF can deal with.

See code below:

view plain print about
1<cfsetting showdebugoutput="false">
2
3<!--- The file to read --->
4<cfset FilePath = "C:\Projects\...\AnImage.jpg">
5
6<!--- Read the file using .Net, returns a byte array. --->
7<cfset objFile = CreateObject("dotnet","System.IO.File")>
8<cfset binaryFileDN = objFile.ReadAllBytes(FilePath)>
9
10<!--- Read[binary] the same file using cffile. --->
11<cffile action="readbinary" file="#FilePath#" variable="binaryFileCF">
12
13<!--- Show the 'lengths' of both objects. --->
14<cfoutput>
15CF binary length: #ArrayLen(binaryFileCF)#<br />
16.Net binary length: #ArrayLen(binaryFileDN)#<br /><br />
17</cfoutput>
18
19<!--- Show the dumps of each object. --->
20<cfdump var="#binaryFileCF#" label="CF"><br />
21<cfdump var="#binaryFileDN#" top="10" label=".Net"><br />
22
23<!--- Take the .Net byte array and convert it to a Base64 string. --->
24<cfset Convert = CreateObject("dotnet","System.Convert")>
25<cfset BodyBase64 = Convert.ToBase64String(binaryFileDN)>
26<cfset newBinaryFileDN = BinaryDecode(BodyBase64, "base64")>
27
28<cfdump var="#newBinaryFileDN#" label=".Net">

The result looks something like this:

Of course this is not limited to just reading binary files, anything that .Net returns as a byte array (e.g. 'System.Net.WebClient' DownloadData()) and consequently gets translated by Java/ColdFusion as an 'Array' can be treated the same way.

ColdFusion, .Net (dotnet) and NTLM

Problem: Trying to make an HTTP request against a web site that has Intergated Windows Authentication (IWA) aka NTLM - sometimes CFHTTP just doesn't cut it.

[More]

How to get local drive information programmatically

Problem: You want to find out how much free space you have on your server's local drives. You also want to find out how much space you have used on your local drives. How can you do this programmatically?

[More]