File uploads in ColdFusion 10's REST API framework

I am trying to upload files to a REST endpoint in ColdFusion 10. I've tried a variety of approaches and none have worked...

  • Specify a CFARGUMENT type="binary" to the CFFUNCTION that is the REST endpoint definition. This results in a CF REST API compile error.
  • Submit a multipart/form-data POST/PUT request and use CFFILE to handle the file upload in the body of the CFFUNCTION. This causes the CF REST framework to not see any of the form-field parameters that are required by CFARGUMENT tags.
  • Uploading the file to a separate endpoint that expects only the file and setting the HTTP content-type header to the file's MIME type. The CF REST framework rejects this because it wants a specific content-type (presumably multipart/form-data or application/x-www-form-urlencoded).
  • The REST endpoint definitions look something like this...

        <cffunction name="createDocument" access="remote" returnType="String" returnformat="JSON" httpMethod="POST" restPath="/document/">
          <cfargument name="Authorization" type="string" required="true" restargsource="Header">
          <cfargument name="folder" type="any" required="true" restargsource="Form">
          <cfargument name="cabinet" type="any" required="true" restargsource="Form">
          <cfargument name="filedata" type="bindary" required="true" restargsource="Form">
         [...]
         </cffunction>
    

    @siromega I am not sure if you ever find a solution for this but I came across taffy.io that supports file uploads. If you did, could you please post an update?

    Headers for the image field are different and content of the image field is in binary encoding.

    https://github.com/atuttle/Taffy/wiki/So-you-want-to:-Upload-a-file-via-your-API

    Hope it helps.


    Recently i have created a file upload API using ColdFusion REST. Here is a sample in which we are accepting a file (csv or excel) and converting it to json. We have registered this REST service in ColdFusion Administrator. The Parameter name against which the file is uploaded is fileParam.

    component  output="false" restpath="/upload"
    {
    
    remote any function uploadFile() httpmethod="POST" consumes="multipart/form-data" produces="application/json" {
        destination = getTempDirectory();       
        uploadDetails = FileUpload(destination, "fileparam", "text/csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "overwrite");
        fileSeparator = "";//make platform independent     
        filePath = uploadDetails.SERVERDIRECTORY & fileSeparator & uploadDetails.ATTEMPTEDSERVERFILE;
        cfspreadsheet(action = "read", src = filePath, excludeHeaderRow = false, query = "data");
        return serializejson(data);
    
    }
    }
    
    链接地址: http://www.djcxy.com/p/31252.html

    上一篇: 从泛型类中返回一个数组

    下一篇: 在ColdFusion 10的REST API框架中上传文件