• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How do I accept file upload through rest api

Guest
Jun 09, 2014 Jun 09, 2014

Copy link to clipboard

Copied

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).

Views

2.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 11, 2014 Jun 11, 2014

Copy link to clipboard

Copied

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

<cfhttpparam mimetype="*">

<cffile accept="*">

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 12, 2014 Jun 12, 2014

Copy link to clipboard

Copied

Did the wildcards help?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 17, 2015 Jun 17, 2015

Copy link to clipboard

Copied

LATEST

Has there been any useful update to this post?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation