Download a note attachment from API

Hi all

I have a webpage that displays my sugar cases to the end user. this pae also lists notes and not attachments for the user to download. However I am having issues with getting sugar to download the file.

when the download link is click it goes to a download.php file to initialise the download and passes the query vars  of the file endpoint

        // [content-type] => application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
        // [content-length] => 59349
        // [name] => Copy of Countries by region.xlsx
        // [uri] => rest/v11/Note/2fe82a46-0efe-11eb-9221-080027633d6c/file/filename
        // [doc_type] => 
        $tokens = MagiConnect_SugarCRM::testConn(); // gets the oauth token for testing

		$curl = curl_init('http://mc.sugarcrm10.mac/rest/v11/CAtts_Case_Attachments/2fe82a46-0efe-11eb-9221-080027633d6c/file/uploadfile');
		curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
		curl_setopt($curl, CURLOPT_HEADER, true);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
		curl_setopt($curl, CURLOPT_HTTPHEADER, array(
				"OAuth-Token: {$tokens->access_token}",
			)
		);

        $response 		= curl_exec($curl);
        // echo "<pre>";
        // print_r($response);
        // echo "</pre>";

		$header_size 	= curl_getinfo($curl,CURLINFO_HEADER_SIZE);
		$headers 		= substr($response, 0, $header_size);
		$file 			= substr($response, $header_size);
		
		$file = substr($response, $header_size);
        file_put_contents('Copy of Countries by region.xlsx', $file);

        curl_close($curl);
this did not initialise the download so i replaced put_file content with force download headers in download.php
        $tokens = MagiConnect_SugarCRM::testConn(); // gets the oauth token for testing

		$curl = curl_init('http://mc.sugarcrm10.mac/rest/v11/CAtts_Case_Attachments/2fe82a46-0efe-11eb-9221-080027633d6c/file/uploadfile');
		curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
		curl_setopt($curl, CURLOPT_HEADER, true);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
		curl_setopt($curl, CURLOPT_HTTPHEADER, array(
				"OAuth-Token: {$tokens->access_token}",
			)
		);

        $response 		= curl_exec($curl);
        // echo "<pre>";
        // print_r($response);
        /// echo "</pre>";

		$header_size 	= curl_getinfo($curl,CURLINFO_HEADER_SIZE);
		$headers 		= substr($response, 0, $header_size);
		$file 			= substr($response, $header_size);

        curl_close($curl);
        
        header("Pragma: public");
        header("Cache-Control: maxage=1, post-check=0, pre-check=0");
        header("Content-Type: application/force-download");
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"Copy of Countries by region.xlsx\";");
        // disable content type sniffing in MSIE
        header("X-Content-Type-Options: nosniff");
        header("Content-Length: 59349");
        header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
        set_time_limit(0);
        @ob_end_clean();
        ob_start();
        readfile($file);
        @ob_flush();
        @unlink($file);
this downloads a corrupt file. Can anyone help me on how i get my download button to download the requested file from sugar?sugar does seem to be returning the correct response in the curl request
Parents Reply Children
No Data