Post

1 follower Follow
0
Avatar

Rest API image upload.

I am having an issue with uploading an image to a Document.

My file is in my project's directory.

$file = 'Eurocopter-EC130.jpg';

$fileInfoArray = array( 
'storage' => 'Server', 
'filename' => $file 
);

$uploadArray = array( 
'documentId' => $documentResultArray['id'], 
'fileInformation' => $fileInfoArray, 
'uploadUrl' => $getUploadResultArray['uploadUrl'], 
);

$ch1 = curl_init(); 
curl_setopt($ch1, CURLOPT_URL, URLCLARIZEN_FILEUPLOAD); 
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch1, CURLOPT_HTTPHEADER, $_SESSION['currentUser']->getAuthenticationHeader()); 
curl_setopt($ch1, CURLOPT_POSTFIELDS, json_encode($uploadArray)); 
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);

$fileUploadResultArray = json_decode(curl_exec($ch1), TRUE);

the API returns 
{"errorCode":"FileNotFound","message":"File not found: Eurocopter-EC130.jpg."

everything else works I am able to 
- Create a Request 
- Create a Document object via the API 
- Create an AttachmentLink between the Request and the new Document

my only issue is populating the document with the file. 
I am using a PHP/Javascript framework and CURL for my API requests.

Thank you.

Mohamed Cherifi Answered

Please sign in to leave a comment.

1 comment

0
Avatar

I found the problem contrary to what the documentation stipulates you should populate the document when creating it. You should add your bytes array to the Document's Content field.

Here's the code:

$file = 'Eurocopter-EC130.jpg';

$handle = fopen($file, "rb");
$fsize = filesize($file);
$contents = fread($handle, $fsize);
$byteArray = unpack("N*",$contents);

$uploadArray = array(
'Name' => $file,
'FileType' => 'Image',
'StorageType' => 'File',
'Content' => $byteArray
);

$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, URLCLARIZEN_DATAOBJECTS . '/' . DOCUMENT);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch1, CURLOPT_HTTPHEADER, $_SESSION['currentUser']->getAuthenticationHeader());
curl_setopt($ch1, CURLOPT_POSTFIELDS, json_encode($uploadArray));
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);

$documentResultArray = json_decode(curl_exec($ch1), TRUE);

// Create attachment link
$uploadArray = array(
'Entity' => $caseResultArray['id'],
'Document' => $documentResultArray['id'],
);

$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL, URLCLARIZEN_DATAOBJECTS . '/' . ATTACHMENT_LINK);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch1, CURLOPT_HTTPHEADER, $_SESSION['currentUser']->getAuthenticationHeader());
curl_setopt($ch1, CURLOPT_POSTFIELDS, json_encode($uploadArray));
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);

$attachmentLinkResultArray = json_decode(curl_exec($ch1), TRUE);
Mohamed Cherifi 0 votes