Documentation

disk.folder.uploadfile

Description

disk.folder.uploadfile

Uploads a new file to specified folder.

In case of a successful upload, it returns the structure similar to disk.file.get.

Parameters

Parameter Description
id Folder ID. You cannot upload file by path folder in current API. You must calculate folder ID.
fileContent Similar to 'DETAIL_PICTURE' in the example Files Processing.
data An array describing the file. New file name is contained in the required NAME field. Can send file as a string encoded in base64.

Examples

Please be advised, you can use REST method disk.rights.getTasks to get the list with available `TASK_ID` identifiers for assigning access permissions:

BX24.callMethod(
        "disk.folder.uploadfile",
        {
            id: 4,
            data: {
                NAME: "avatar.jpg"
            },
            fileContent: document.getElementById('test_file_input'),
			generateUniqueName: true,
			rights: [
				{
					TASK_ID: 42,
					ACCESS_CODE: 'U35' //access for user with ID=35. You can get name for type of access by using https://training.bitrix24.com/rest_help/general/user_access.php
                },
				{
					TASK_ID: 38,
					ACCESS_CODE: 'U2' //access for user with ID=35. You can get name for type of access by using https://training.bitrix24.com/rest_help/general/user_access.php
                }
            ]
        },
        function (result)
        {
            if (result.error())
                console.error(result.error());
            else
                console.dir(result.data());
        }
);

Exampe of a direct file upload to Drive24

  1. First, we should call /rest/disk.folder.uploadFile and pass to the method only the ID of the folder
  2. disk.folder.uploadFile?auth=n2423m863oil59f99c9g0bm4918l5erz&id=289
    
  3. As the response we receive the UploadUrl parameter and the field parameter:
  4. "result": {
            "field": "file",  
            "uploadUrl": "http://b24.sigurd.bx/rest/upload.json?auth=n2423m863oil59f99c9g0bm4918l5erz&token=disk%7CaWQ9Mjg5Jl89QkYzazEzaXNnUjNHcVZQcDJZaGxGRmI4TGhXOG5EZXQ%3D%7CInVwbG9hZHxkaXNrfGFXUTlNamc1Smw4OVFrWXphekV6YVhOblVqTkhjV
    lpRY0RKWmFHeEdSbUk0VEdoWE9HNUVaWFE9fG4yNDIzbTg2M29pbDU5Zjk5YzlnMGJtNDkxOGw1ZXJ6Ig%3D%3D.Aga709nyY0%2BrFiv3laHjfg6XuOO5JT6ttjU%2F53ifphM%3D"
        }
    
  5. To the received UploadUrl, send POST-request to multipart/form-data, where we pass the file to the field with the name, received in the field parameter:
  6. http --form POST "http://b24.sigurd.bx/rest/upload.json?auth=n2423m863oil59f99c9g0bm4918l5erz&token=disk%7CaWQ9Mjg5Jl89QkYzazEzaXNnUjNHcVZQcDJZaGxGRmI4TGhXOG5EZXQ%3D%7CInVwbG9hZHxkaXNrfGFXUTlNamc1Smw4OVFrWXp
    hekV6YVhOblVqTkhjVlpRY0RKWmFHeEdSbUk0VEdoWE9HNUVaWFE9fG4yNDIzbTg2M29pbDU5Zjk5YzlnMGJtNDkxOGw1ZXJ6Ig%3D%3D.Aga709nyY0%2BrFiv3laHjfg6XuOO5JT6ttjU%2F53ifphM%3D" file@~/somelongfile.log
    
  7. As a response, we receive data about the uploaded file:
  8. "result": {
            "CODE": null,  
            "CREATED_BY": "1",  
            "CREATE_TIME": "2016-03-30T14:30:41+02:00",  
            "DELETED_BY": null,  
            "DELETED_TYPE": 0,  
            "DELETE_TIME": null,  
            "DETAIL_URL": "http://b24.sigurd.bx/company/personal/user/1/disk/file/Тестируем REST/somelongfile.log",  
            "DOWNLOAD_URL": "http://b24.sigurd.bx/rest/download.json?auth=n2423m863oil59f99c9g0bm4918l5erz&token=disk%7CaWQ9MjkwJl89ZTI4MG9TcDZCQno2MDAwVmV3cnRkbWxLM2hLN0JweEs%3D%7CImRvd25sb2FkfGRpc2t8YVdROU1qa3dKbDg5WlRJNE1HOVRjRFpD
    UW5vMk1EQXdWbVYzY25Sa2JXeExNMmhMTjBKd2VFcz18bjI0MjNtODYzb2lsNTlmOTljOWcwYm00OTE4bDVlcnoi.QlpUpx4mG9sxeyMyholPfdgkoXgc9kK9gtbOagqSo7s%3D",
            "FILE_ID": 209,  
            "GLOBAL_CONTENT_VERSION": 1,  
            "ID": 290,  
            "NAME": "somelongfile.log",  
            "PARENT_ID": "289",  
            "SIZE": "496136787",  
            "STORAGE_ID": "1",  
            "TYPE": "file",  
            "UPDATED_BY": "1",  
            "UPDATE_TIME": "2016-03-30T14:30:43+02:00"
        }
    

How to upload file using UploadUrl via PHP

 $folderId,
		]
	);
	if (!empty($file['result']['uploadUrl']))
	{
		$info = pathinfo($path);
		if ($info['basename'])
		{
			$delimiter = '-------------' . uniqid('', true);
			$name = $info['basename'];
			$mime = mime_content_type($path);
			$content = file_get_contents($path);

			$body = '--' . $delimiter. "\r\n";
			$body .= 'Content-Disposition: form-data; name="file"';
			$body .= '; filename="' . $name . '"' . "\r\n";
			$body .= 'Content-Type: ' . $mime . "\r\n\r\n";
			$body .= $content . "\r\n";
			$body .= "--" . $delimiter . "--\r\n";

			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $file['result']['uploadUrl']);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
			curl_setopt(
				$ch,
				CURLOPT_HTTPHEADER,
				[
					'Content-Type: multipart/form-data; boundary=' . $delimiter,
					'Content-Length: ' . strlen($body),
				]
			);
			$out = curl_exec($ch);
			try
			{
				$result = json_decode($out, true, 512, JSON_THROW_ON_ERROR);
			}
			catch (JsonException $e)
			{
				$result = [
					'error' => $e->getMessage(),
				];
			}
		}
	}
}

echo '<pre>';
	print_r($result);
echo '</pre>';


© «Bitrix24», 2001-2024
Up