Download Field Attachments

Purpose

To download the attachments from the file upload and image upload fields of a module.

Request Details

Request URL

{api-domain}/crm/{version}/{module_api_name}/{record_ID}/actions/download_fields_attachment

Supported modules

Leads, Accounts, Contacts, Deals, Tasks, Meetings, Calls, Products, Price Books, Quotes, Sales Orders, Purchase Orders, Invoices, Campaigns, Vendors, Cases, Solutions, and Custom

Header

Authorization: Zoho-oauthtoken <access_token>

Scope

scope=ZohoCRM.modules.{module_name}.READ

Parameters
  • fields_attachment_idstring, mandatory

    The unique ID of the attachment of either the file upload field or the image upload field in the module. You can obtain this ID using the Get Records API for that specific record.

Possible Errors

  • INVALID_DATAHTTP 400

    The ID of the record is invalid.
    Resolution: Specify the correct record ID.

  • NO_PERMISSIONHTTP 403

    You do not have permission to download the attachments of the record in this module.
    Resolution: Contact your administrator.

Sample Request

Copiedcurl "https://www.zohoapis.com/crm/v2.1/Leads/554023000001054001/actions/download_fields_attachment?fields_attachment_id=554023000001736007"
-X GET
-H "Authorization: Zoho-oauthtoken 1000.03xxxxxxxxxxxxxxxxxa5317.dxxxxxxxxxxxxxxxxxfa"
4.0.04.x
Copied//Get instance of FieldAttachmentsOperations Class
$fieldAttachmentsOperations = new FieldAttachmentsOperations($moduleAPIName, $recordId, $fieldsAttachmentId);
//Call getFieldAttachments method
$response = $fieldAttachmentsOperations->getFieldAttachments();
Copied<?php
class GetFieldAttachments{
    
    public function execute(){
        $curl_pointer = curl_init();
        
        $curl_options = array();
        $url = "https://www.zohoapis.com/crm/v2.1/Leads/554023000001054001/actions/download_fields_attachment?";
        $parameters = array();
        $parameters["id"]="f46166fa14ce16c6e2622b3ce8283075efea966da0bddc9edf7421c6bb22cc17";
        
        foreach ($parameters as $key=>$value){
            $url =$url.$key."=".$value."&";
        }

        $curl_options[CURLOPT_URL] = $url;
        $curl_options[CURLOPT_RETURNTRANSFER] = true;
        $curl_options[CURLOPT_HEADER] = 1;
        $curl_options[CURLOPT_CUSTOMREQUEST] = "GET";
        $headersArray = array();
        $headersArray[] = "Authorization". ":" . "Zoho-oauthtoken " ."1000.30f3a589XXXXXXXXXXXXXXXXXXX4077.dc5XXXXXXXXXXXXXXXXXXXee9e7c171c";
        
        $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);
            }
        }
        $response = $content;
        if ($response == null && $responseInfo['http_code'] != 204) {
            list ($headers, $content) = explode("\r\n\r\n", $content, 2);
            $response = json_decode($content, true);
        }
        $contentDisp = $headerMap['Content-Disposition'];
        $fileName = substr($contentDisp, strrpos($contentDisp, "'") + 1, strlen($contentDisp));
        
        if (strpos($fileName, "=") !== false)
        {
            $fileName = substr($fileName, strrpos($fileName, "=") + 1, strlen($fileName));
            
            $fileName = str_replace(array(
                '\'',
                '"'
            ), '', $fileName);
        }
        $filePath = "/Users/test/PHP/PHPNativeSampleCode/";
        $fp = fopen($filePath . $fileName, "w"); // $filePath - absolute path where downloaded file has to be stored.
        $stream = $response;
        fputs($fp, $stream);
        fclose($fp);   
        
    }
    
}
(new GetFieldAttachments())->execute();