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

Fullscreen
1
2
3
4
5
// [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] =>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$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);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
this did not initialise the download so i replaced put_file content with force download headers in download.php
Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$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);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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
  • Hi ,

    You already have the file contents as part of $file - which is a string, not a file handle.

    So all you have to do is to do echo $file instead of readfile($file).

    You can remove the @unlink($file) too.

    For the output headers, you can read the Content-Type, Content-Length and Content-Disposition from the Sugar response.