AnsweredAssumed Answered

Uploaded files get corrupted in sugar

Question asked by john Fieldsend on Mar 1, 2020
Latest reply on Mar 1, 2020 by john Fieldsend

Hi All, 


I am trying to store a custom image file to sugar with a unique file name to a filename field on a custom Module (holo_FeatureKey).


there is a file size difference from the original file and the one then stored in sugar


org file: 2f5f5f47b2aecbcc6c376a0f771254ec.jfif 271kb

sugar file: 2f5f5f47b2aecbcc6c376a0f771254ec.jfif 178kb


our jfif files are jpegs with specific headeres within the files that are read by our printers


I could do this when we had salesforce by sending the image as a base64 encoded file but not sure how you accomplish this with sugar.


I currently use a an insertattachment function to send files to {sugar}/rest/v11/{module}/{id}/file/filename

working example:/rest/v11/holo_FeatureKey/184f1804-5be9-11ea-bc9d-080027633d6c/file/filename


my helper function from my laravell website to send file to sugar

          public static function insertattachment($module, $id, $filepath, $filename = '', $mime = '') 

               $url = "{$module}/{$id}/file/filename";
               $file_arguments = array(
                    "format"                => "sugar-html-json",
                    "delete_if_fails"      => true,
                    "oauth_token"           => self::$tokens->access_token,
                "filename"               => new \CurlFile($filepath, $mime, $filename),

               $curl = curl_init(self::$SUGARENDPOINTURI.self::$API.$url);
               curl_setopt($curl, CURLOPT_HEADER, false);
               curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
               curl_setopt($curl, CURLOPT_POST, true);
               curl_setopt($curl, CURLOPT_POSTFIELDS, $file_arguments);
               curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                         "Authorization: Bearer",
                         "OAuth-Token: ".self::$tokens->access_token,
                         "Content-Type: multipart/form-data;",

               $json_response = curl_exec($curl);
               $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

               return $json_response;


this is called by my create featurekey function which if created successfully sends the id to the insertattachent function

    public static function createHkKey($contact_id, $serial_id, $serial_number, $path, $filename)
        $result = array('success' => false, 'attachment' => false);
        $args = array(
            'contactID'     => $contact_id,
            'serialID'      => $serial_id,
            'name'          => $serial_number.': Holokote',
            'status'        => 'Ordered',
            'type'          => 'Holokote',
            'description'   => 'Custome Holokote for serial '.$serial_number,

        $url    = 'FeatureKey/Create';
        $create = SugarCRM::insertcrm(json_encode($args), $url);
        $result['create'] = $create;

        if ($create->success == true) {
            $result['success'] = true;
            $attachment = SugarCRM::insertattachment('holo_FeatureKey', $create->fkey_id, $path, $filename, 'image/jpeg');
            return $result;
        $result['error'] = $create->errors;
        return $result;


I am not sure how I send an encoded base64 file. 


My old salesforce function was 

          $create_featurekey = \App\Salesforce::create('FeatureKey__c', json_encode($featurekey));
          // \Log::info(json_encode($create_featurekey, JSON_UNESCAPED_SLASHES));
          if (isset($create_featurekey->success)) {

               $imagedata = file_get_contents($keypath);

               $attachment = json_encode(array(
                    'Name'                => basename($keypath),
                    'ContentType'      => 'image.jpeg',
                    'Body'                => base64_encode($imagedata),
                    'Description'     => $name,
                    'IsPrivate'          => false,
                    'ParentId'           => $create_featurekey->id,

               $attachment_result = \App\Salesforce::create('Attachment', $attachment);         

               if ($attachment_result->success == true) {
                    return $create_featurekey->id;
               } else {
                    return false;

         } else {
               return false;

this sent the file to salesforce as a base64 encoded file