在ColdFusion中使用Base64编码发布文件数据

时间:2014-06-27 18:01:41

标签: coldfusion coldfusion-10 cfhttp

我正在尝试发送包含Base64编码的二进制文件的xml。当我调用cfhttp时,我的ColdFusion 10服务器一直挂起,直到超时。 Visual Studio即时调试程序不断弹出。

我正在使用来自this帖子的Ben Nadel教程。有人可以帮忙吗?

  <cffunction name="_push" access="public" returntype="Any">
<!---
  Create the path to the binary file that we want to
  post via a web service.
--->
<cfset strFilePath = ExpandPath( './myfile.zip' ) />



<!--- Read in the binary file. --->
<cffile
  action="readbinary"
  file="#strFilePath#"
  variable="objBinaryData"
  />


<!---
  Create the XML that we are going to post. When posting this
  file, we are going to encode it as base64 text data.
--->
<!--- <cfsavecontent variable="strXmlData"> --->
<cfxml variable="strXmlData">
  <cfoutput>

    <file>
      <name>#XmlFormat(
        ListLast( strFilePath, "\/" )
        )#</name>

      <ext>#XmlFormat(
        ListLast( strFilePath, "." )
        )#</ext>

      <!---
        When storing the binary data, we are going to
        pass the file as a Base64 encoding. I am like
        95% sure that this will result in only valid
        XML characters, but I am not completely sure.
      --->
      <base64>#BinaryEncode(objBinaryData,"Base64")#</base64>
    </file>
  </cfoutput>
</cfxml>
<!--- </cfsavecontent> --->


<!---
  Build the URL for the "web service" to which we are going
  to post this file data.
--->
<cfset strUrl = (
  GetDirectoryFromPath(
    GetPageContext().GetRequest().GetRequestUrl().ToString()
    ) &
  "webservice.cfm"
  ) />


<cftry>

  <!--- Post the XML data to the "web service" file. --->
  <cfhttp
    url="#strUrl#"
    result="httpresponse"
    method="post"
    throwOnError="yes"
    timeout="60">

    <!--- Post XML data. --->
    <cfhttpparam
      type="xml"
      value="#strXmlData#"
      name="myxml"
      />

  </cfhttp>               

   <cfcatch type="any">
      <cfdump var="#cfcatch#" output="c:/dump-a.txt">
      <cfset httpresponse = cfcatch>
   </cfcatch>
</cftry> 


 <cfreturn httpresponse>

这是webservice.cfm文件内容

  <!--- Grab the content from the request data post. --->
  <cfset strContent = Trim( GetHttpRequestData().Content ) />
  <cfdump var="#strContent#" output="c:/webs/dump-b.txt">


  <!--- Check to see if the content is a valid XML document. --->
  <cfif IsXml( strContent )>
     <!---
        Parse the XML data into a ColdFusion Xml
        Document Object.
     --->
     <cfset xmlPost = XmlParse( strContent ) />

     <!---
        Check to see if we have all the XML nodes that we
        need in order to save the file. For our naming
        purposes, all we need is the file extension and
        the base64 data.
     --->
     <cfif (
        StructKeyExists( xmlPost.file, "ext" ) AND
        StructKeyExists( xmlPost.file, "base64" )
        )>

        <!---
           ASSERT: We have the nodes that we need, and for
           this demo, we are just going to assume that the
           data values are valid.
        --->

        <!---
           Create the file name for the target file. We are
           going to use the passed in extension and a UUID.
           Make sure that file name points to the executing
           script directory (ExpandPath()).
        --->
        <!---
        <cfset strFileName = ExpandPath(
           CreateUUID() & "." &
           xmlPost.file.ext.XmlText
           ) />
           --->
        <cfset ExpandPath( CreateUUID()&'.'& xmlPost.file.ext.XmlText) />

        <!---
           Grab the base64 file data and convert it to
           binary data.
        --->
        <cfset objBinaryData = ToBinary(
           xmlPost.file.base64.XmlText
           ) />


        <!--- Write the binary file data to disk. --->
        <cffile
           action="write"
           file="#strFileName#"
           output="#objBinaryData#"
           />           

     </cfif>

  </cfif>     

我正在使用在Windows 8和ColdFusion 10(开发人员版)上运行的开发机器上进行测试。

0 个答案:

没有答案