File Processing
The REST methods accept files as a base64 encoded string. Another option is to pass a simple array with the file name as the first array item, and the base64 encoded file data as the second item.
With a fully client application, you can use a FileReader object instance, or pass a reference to a file HTML form element (<input type="file">
) as the request field value.
Example
<input type="file" id="testfile"><br /> <span onclick="sendInputFile()">send file from input</span><br /> <span onclick="sendStaticFile()">send static file</span><br /> <script type="text/javascript"> function sendInputFile() { BX24.callMethod('entity.item.add', { 'ENTITY': 'menu', 'NAME': Math.random(), 'DETAIL_PICTURE': document.getElementById('testfile') }, function(){ alert('Finished!'); }); } /* POST https://my.bitrix24.com/rest/entity.item.add.json HTTP/1.1 Host: my.bitrix24.com Content-Length: 186 Content-Type: text/plain; charset=UTF-8 auth=6a8c365cb010ba42bd5b0f6ae803f47c&ENTITY=menu&NAME=0.2630483947652045&DETAIL_PICTURE[0]=1.gif&DETAIL_PICTURE[1]=R0lGODlhAQABAIAAAP%2F%2F%2FwAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D */ function sendStaticFile() { BX24.callMethod('entity.item.add', { 'ENTITY': 'menu', 'NAME': '1.gif', 'DETAIL_PICTURE': ['1.gif', '=base64_encode(file_get_contents("1.gif"))?>'] }, function(){ alert('Finished!'); }); } /* POST https://my.bitrix24.com/rest/entity.item.add.json HTTP/1.1 Host: my.bitrix24.com Content-Length: 173 Content-Type: text/plain; charset=UTF-8 auth=6a8c365cb010ba42bd5b0f6ae803f47c&ENTITY=menu&NAME=1.gif&DETAIL_PICTURE[0]=1.gif&DETAIL_PICTURE[1]=R0lGODlhAQABAIAAAP%2F%2F%2FwAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D */ </script>
For CRM methods, when adding a picture for a product, instead of
'DETAIL_PICTURE': ['1.gif', 'R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==']
use
"PREVIEW_PICTURE": {"fileData": ["1.gif", "R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="]}
Such feature is based on the fact that CRM supports file deletion.
Deleting image from a field you need to get image file ID first inside the PHOTO field using the method crm.contact.get and then pass it with the 'remove' parameter in the method crm.contact.update, as in example, photo ID 11062 is deleted in contact 308 (REGISTER_SONET_EVENT may be skipped and not passed):
BX24.callMethod( "crm.contact.update", { id: 308, fields: { "PHOTO": {id: 11062, remove: 'Y'} }, params: { "REGISTER_SONET_EVENT": "Y" } }, function(result) { if(result.error()) console.error(result.error()); else { console.info(result.data()); } } );