0 Replies Latest reply on Jul 15, 2008 7:26 AM by Coos

    How do I use a ByteArray or MemoryStream from a .Net assembly

    Coos Level 1
      We created an assembly that returns an Excel file as either a ByteArray or MemoryStream and need to be able have CF either save the file on the server hard drive or prompt the user to open or download the file from the browser. The assembly simply calls SQL Reporting Services. An .aspx page that calls the same assembly is able to prompt the user to download the file in the browser and a formatted report displays.

      If we cfdump the ByteArray, we can see 24,000 elements in it.

      <!--- call the assembly--->
      <cfobject type = ".net" name = "GeneratorInstance" class = "ReportGenerator.Generator"
      assembly = "C:\ReportGenerator\ReportGenerator.dll">

      <!--- this correctly shows all the methods of my assembly--->
      <cfdump var="#GeneratorInstance#">

      <!--- call the ExcelStream method--->
      <cfset a = GeneratorInstance.ExcelStream()>

      <!--- this correctly dumps the bytearray as a plain old array with 24,000 elements in it--->
      <cfdump var="#a#">

      <!---These actions produce an Excel file that's only 22 bytes. --->
      <cffile action="write" addnewline="no" file="C:\test.xls" output="#a#" />
      <cffile action="write" addnewline="no" file="C:\test.xls" output="#DotNetToCFType(a)#" />
      <cfscript>
      FileWrite("C:\test.xls",a);
      </cfscript>

      <!--- I would normally use this code to stream an Excel file to the browser--->
      <cfheader name="content-disposition" value="inline; filename=testing.xls" />
      <cfcontent type="application/xls" variable="#a#" />

      I believe I have to somehow convert the ByteArray into a binary file but I'm not sure how to go about this.

      Here's some other failed attempts that seem to just hang the browser:
      <cfscript>
      context = getPageContext();
      context.setFlushOutput(false);
      response = context.getResponse().getResponse();
      out = response.getOutputStream();
      response.setContentType("application/xls");
      response.setContentLength(arrayLen(DotNetToCFType(a)));
      out.write(DotNetToCFType(a));
      out.flush();
      response.reset();
      out.close();
      </cfscript>

      <cfscript>
      context = getPageContext();
      context.setFlushOutput(false);
      response = context.getResponse().getResponse();
      out = response.getOutputStream();
      response.setContentType("application/xls");
      response.setContentLength(arrayLen(a));
      out.write(a);
      out.flush();
      out.close();
      </cfscript>

      <!--- An lastly, this one just writes the same file as the other <cffile> operations--->
      <cfscript>
      saveToFile = "C:\testing.xls";
      outStream = createObject("java", "java.io.FileOutputStream").init( saveToFile );
      outStream.write( a );
      outStream.flush();
      // always close the stream
      outStream.close();
      </cfscript>