Skip to main content
Jun 9, 2014 3:07 PM UTC
Question

How do I accept file upload through rest api

I am attempting to implement a REST endpoint that accepts a multi-part form request that includes form data as well as a file upload. Is that even possible? Is it possible to just accept a file upload and nothing else (e.g. specify a unique ID in the URL and POST/PUT data in the request body)? I've tried numerous things and have been unable to successfully implement a REST endpoint that accepts a file upload...

  • 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).
This topic is closed to new replies. Start a new post to keep the conversation going.

5 replies

Known Participant
Jun 17, 2015 2:39 PM UTC

Has there been any useful update to this post?

Translate
BKBK
Community Expert
Community Expert
Jun 11, 2014 9:10 AM UTC

The following method, unlike those above, will work. It requires you to know the MIME type of the binary file you wish to upload. This example uploads a PDF document.

uploader.cfm

<cfset restInitApplication(expandPath("."))>

<cfhttp url="http://localhost:8500/rest/myRestApp/myService" method="post">

    <cfhttpparam file="C:\temp\testDoc.pdf" mimetype="application/pdf" name="myFile" type="file">

</cfhttp>

service.cfc

<cfcomponent rest="true" restpath="/myService">

<cffunction name="getFile" access="remote" httpMethod="POST" returntype="string">

<cffile action="upload" accept="application/pdf" filefield="myFile" nameconflict="overwrite" destination="C:\Users\BKBK\Desktop\testFile.pdf">

<cfreturn "File uploaded!">

</cffunction>

</cfcomponent>

Translate
Jun 11, 2014 4:18 PM UTC

We need to allow all file types to be uploaded. We cannot constrain ourselves to just a few file types.

Translate
BKBK
Community Expert
Community Expert
Jun 11, 2014 6:43 PM UTC

No problem at all. Use the wildcard for MIME type, as follows:

<cfhttpparam mimetype="*">

<cffile accept="*">

Translate