Upload Files to Zoho File System (ZFS)

Purpose

ZFS is the central storage system to all your files and attachments. Use this API to upload fields to ZFS. This API returns an encrypted file ID and the file name. You can use this encrypted ID to attach this file to a file/image upload field or a record in Zoho CRM. You must upload the file with content type as multipart/form data.

Endpoints

Request Details

Request URL

{api-domain}/crm/{version}/files

Header

Authorization: Zoho-oauthtoken d92d4xxxxxxxxxxxxx15f52

Scope

scope=ZohoCRM.Files.{operation_type}

Possible operation types

CREATE - To add files to ZFS

Parameters

  • typestring, optional

    To upload inline images.
    Resolution: The value is inline.

Sample Request

Copiedcurl "https://www.zohoapis.com/crm/v2.1/files"
-X POST
-H "Authorization: Zoho-oauthtoken 1000.8cb99dxxxxxxxxxxxxx9be93.9b8xxxxxxxxxxxxxxxf"
-F "file=@connections.txt"
-F "file=@enable-signals.png"
4.0.02.1.0
Copied//Get instance of RecordOperations Class
$fileOperations = new FileOperations();
//Get instance of FileBodyWrapper Class that will contain the request body
$bodyWrapper = new BodyWrapper();
//Get instance of StreamWrapper class that takes absolute path of the file to be attached as parameter
$streamWrapper = new StreamWrapper(null, null, "/Users/username/Desktop/py.html");
//Get instance of StreamWrapper class that takes absolute path of the file to be attached as parameter
$streamWrapper1 = new StreamWrapper(null, null, "/Users/username/Desktop/download.png");
//Get instance of StreamWrapper class that takes absolute path of the file to be attached as parameter
$streamWrapper2 = new StreamWrapper(null, null, "/Users/username/Desktop/samplecode.txt");
//Set file to the FileBodyWrapper instance
$bodyWrapper->setFile([$streamWrapper, $streamWrapper1, $streamWrapper2 ]);
//Get instance of ParameterMap Class
$paramInstance = new ParameterMap();
//Call uploadFiles method that takes BodyWrapper instance as parameter.
$response = $fileOperations->uploadFiles($bodyWrapper, $paramInstance);
Copied<?php

class UploadFilestoZohoFileSystem
{
	public function execute(){
        $curl_pointer = curl_init();
        
        $curl_options = array();
        $curl_options[CURLOPT_URL] = "https://www.zohoapis.com/crm/v2.1/files";
        $curl_options[CURLOPT_RETURNTRANSFER] = true; 
        $curl_options[CURLOPT_HEADER] = 1;
        $curl_options[CURLOPT_CUSTOMREQUEST] = "POST";
        $fileName = "asdad.zip";
        $filePath = "/Users/test/PHP/PHPNativeSampleCode/asdad.zip";
        $file = fopen($filePath, "rb");
        $fileData = fread($file, filesize($filePath));
        $date = new \DateTime();
        
        $current_time_long= $date->getTimestamp();
        
        $lineEnd = "\r\n";
        
        $hypen = "--";
        
        $contentDisp = "Content-Disposition: form-data; name=\""."file"."\";filename=\"".$fileName."\"".$lineEnd.$lineEnd;
        
        
        $data = utf8_encode($lineEnd);
        
        $boundaryStart = utf8_encode($hypen.(string)$current_time_long.$lineEnd) ;
        
        $data = $data.$boundaryStart;
        
        $data = $data.utf8_encode($contentDisp);
        
        $data = $data.$fileData.utf8_encode($lineEnd);
        
        $boundaryend = $hypen.(string)$current_time_long.$hypen.$lineEnd.$lineEnd;
        
        $data = $data.utf8_encode($boundaryend);
        
        $curl_options[CURLOPT_POSTFIELDS]= $data;
        $headersArray = array();
        
        $headersArray = ['ENCTYPE: multipart/form-data','Content-Type:multipart/form-data;boundary='.(string)$current_time_long];
        $headersArray[] = "content-type".":"."multipart/form-data";
        $headersArray[] = "Authorization". ":" . "Zoho-oauthtoken " ."1000.30f3a589XXXXXXXXXXXXXXXXXXX4077.dc5XXXXXXXXXXXXXXXXXXXee9e7c171c";
        var_dump($data);

        $curl_options[CURLOPT_HTTPHEADER]=$headersArray;
        
        curl_setopt_array($curl_pointer, $curl_options);

        $result = curl_exec($curl_pointer);
        $responseInfo = curl_getinfo($curl_pointer);
        curl_close($curl_pointer);
        list ($headers, $content) = explode("\r\n\r\n", $result, 2);
        if(strpos($headers," 100 Continue")!==false){
            list( $headers, $content) = explode( "\r\n\r\n", $content , 2);
        }
        $headerArray = (explode("\r\n", $headers, 50));
        $headerMap = array();
        foreach ($headerArray as $key) {
            if (strpos($key, ":") != false) {
                $firstHalf = substr($key, 0, strpos($key, ":"));
                $secondHalf = substr($key, strpos($key, ":") + 1);
                $headerMap[$firstHalf] = trim($secondHalf);
            }
        }
        $jsonResponse = json_decode($content, true);
        if ($jsonResponse == null && $responseInfo['http_code'] != 204) {
            list ($headers, $content) = explode("\r\n\r\n", $content, 2);
            $jsonResponse = json_decode($content, true);
        }
        var_dump($headerMap);
        var_dump($jsonResponse);
        var_dump($responseInfo['http_code']);
       
    }
    
}
(new UploadFilestoZohoFileSystem())->execute();
2.1.0
Copied# Get instance of FileOperations Class
fo = Files::FileOperations.new
# Get instance of FileBodyWrapper Class that will contain the request body
bw = Files::BodyWrapper.new
# """
# StreamWrapper can be initialized in any of the following ways

# * param 1 -> fileName
# * param 2 -> Read Stream.
# * param 3 -> Absolute File Path of the file to be attached
# """
# stream_wrapper = StreamWrapper.new(name,stream,absolutefilepath)
# Get instance of ParameterMap Class
pm = ParameterMap.new
pm.add(Files::FileOperations::UploadFileParam.type, 'inline')
sw1 = Util::StreamWrapper.new(nil, nil, '/Users/user_name/RUBYWS/zohocrm-rubysdk-sample-application/3524033000005520002.zip')
sw2 = Util::StreamWrapper.new(nil, nil, '/Users/user_name/RUBYWS/zohocrm-rubysdk-sample-application/3524033000005902005.zip')
# Set list of files to the BodyWrapper instance
files = [sw1, sw2]
bw.file = files
# Call upload_file method that takes BodyWrapper instance as parameter.
response = fo.upload_file(bw, pm)
Copiedrequire 'net/http'
require 'json'

class UploadFilestoZohoFileSystem 

    def execute
      
        url = "https://www.zohoapis.com/crm/v2.1/files"
        url = URI(url)
        req = Net::HTTP::Post.new(url.request_uri)
        http = Net::HTTP.new(url.host, url.port)
        http.use_ssl = true
        headers={}
        headers["Authorization"]="Zoho-oauthtoken 1000.dfa7XXXXXXXXXXXXXXXXXX84f9665840.c176aeXXXXXXXXXXXX13f3d37a84d"
        headers&.each { |key, value| req.add_field(key, value) }
        file_path = "/Users/test/Desktop/pic.png"
        file = File.open(file_path)
        data=[["file",file.read, { filename: "pic.png" }]]
        req.set_form(data,'multipart/form-data')
        response=http.request(req)
        status_code = response.code.to_i
        headers = response.each_header.to_h
        print status_code
        print headers
        unless response.body.nil?
            print  response.body
        end
    end
end
UploadFilestoZohoFileSystem.new.execute
CopiedfileVariable = invokeurl
[
	url: "https://file-examples-com.github.io/uploads/2017/10/file-sample_150kB.pdf"
	type: GET
];
fileVariable.setParamName("file");
response = invokeurl
[
	url :"https://www.zohoapis.com/crm/v2.1/files"
	type :POST
	files: fileVariable
	connection:"crm_oauth_connection"
];
info response;

Possible Errors

  • failure_in_attachment_handling HTTP 400

    invalid oauth scope to access this URL
    Resolution: Problem in uploading attachment. kindly upload the file properly

  • VIRUS_DETECTEDHTTP 400

    Unable to process your request. Virus has been detected.
    Resolution: A virus is found in the file that you uploaded. Upload a file without any viruses.

  • INVALID_URL_PATTERNHTTP 404

    Please check if the URL trying to access is a correct one
    Resolution: The request URL specified is incorrect. Specify a valid request URL. Refer to request URL section above.

  • OAUTH_SCOPE_MISMATCHHTTP 401

    Unauthorized
    Resolution: Client does not have ZohoCRM.Files.CREATE scope. Create a new client with valid scope. Refer to scope section above.

  • NO_PERMISSIONHTTP 403

    Permission denied to create
    Resolution: The user does not have permission to upload files to ZFS. Contact your system administrator.

  • INTERNAL_ERRORHTTP 500

    Internal Server Error
    Resolution: Unexpected and unhandled exception in Server. Contact support team.

  • INVALID_REQUEST_METHODHTTP 400

    The http request method type is not a valid one
    Resolution: You have specified an invalid HTTP method to access the API URL. Specify a valid request method. Refer to endpoints section above.

  • AUTHORIZATION_FAILEDHTTP 400

    User does not have sufficient privilege to create.
    Resolution: The user does not have the permission to add files to ZFS. Contact your system administrator.

Note
  • The maximum size of each attachment is 20MB.

Sample Response

Copied{
    "data": [
        {
            "code": "SUCCESS",
            "details": {
                "name": "connections.txt",
                "id": "2cceafaxxxxxxxc065186"
            },
            "message": "connections.txt uploaded Succeessfully",
            "status": "success"
        },
        {
            "code": "SUCCESS",
            "details": {
                "name": "enable-signals.png",
                "id": "2ccxxxxxxxxxxxb89e051a"
            },
            "message": "enable-signals.png uploaded Succeessfully",
            "status": "success"
        }
    ]
}