Download the voting report vote.AttachedVote.download
Scope:
voteWho can execute the method: user with read access permission for voting
The method vote.AttachedVote.download generates and provides a downloadable report for the vote in the specified format.
Method Features
Attention! This method is an exception to the general rule of working with the REST API. Unlike other methods, it returns not a JSON object, but the actual content of the file with HTTP headers that initiate the download in the browser.
Due to this, the standard function BX24.callMethod() cannot process the response and will throw an error. To work with this method, a direct HTTP request must be made, as shown in the JS code example.
Method Parameters
There are three options for calling the method.
1. By the ID of the attached vote
Required parameters are marked with *
|
Name |
Description |
|
attachId* |
The ID of the attached vote, which can be obtained using the methods vote.AttachedVote.get or vote.AttachedVote.getMany |
2. By the entity with the vote
Required parameters are marked with *
|
Name |
Description |
|
moduleId* |
The module ID, possible values:
|
|
entityType* |
The object type, possible values:
|
|
entityId* |
The ID of the entity, possible values:
|
3. By the signed ID
Required parameters are marked with *
|
Name |
Description |
|
signedAttachId* |
The signed ID of the attachment, which can be obtained using the method vote.AttachedVote.get, response parameter |
Code Examples
How to Use Examples in Documentation
curl -X POST \
-L \
-o **put_file_name**.xls \
-H "Content-Type: application/json" \
-d '{"attachId":**put_attach_id**}' \
"https://**put_your_bitrix24_address**/rest/**put_your_user_id_here**/**put_your_webhook_here**/vote.AttachedVote.download"
curl -X POST \
-L \
-o **put_file_name**.xls \
-H "Content-Type: application/json" \
-d '{"attachId":**put_attach_id**, "auth": "**put_access_token_here**"}' \
"https://**put_your_bitrix24_address**/rest/vote.AttachedVote.download"
function downloadVoteReportById(attachId)
{
// 1. Get authorization data from the BX24 library
const auth = BX24.getAuth();
if (!auth)
{
return;
}
// 2. Form the URL for the direct request to the REST API
const restUrl = new URL(`https://${auth.domain}/rest/vote.AttachedVote.download`);
restUrl.searchParams.append('auth', auth.access_token);
restUrl.searchParams.append('attachId', attachId);
console.log(`Download request: ${restUrl}`);
// 3. Execute the request and process the response as a binary file (blob)
fetch(restUrl)
.then(response => {
if (!response.ok)
{
// If the request failed, try to process the standard JSON error from Bitrix24
return response.json().catch(() => {
// If the response body is not JSON, throw a general network error
throw new Error(`Network error: ${response.status} ${response.statusText}`);
}).then(errorData => {
// If able to parse the JSON error
throw new Error(`API error: ${errorData.error_description || 'Unknown error'}`);
});
}
// On success, get the data as a binary object
return response.blob();
})
.then(blob => {
// 4. Create an "invisible" link and initiate the download in the browser
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
// Set the file name that the user will see
a.download = `vote_report_${attachId}.${fileType}`;
document.body.appendChild(a);
a.click();
// Clean up temporary data
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
})
.catch(error => {
console.error('Error downloading the report:', error);
alert(`Failed to download the report: ${error.message}`);
});
}
<?php
// This file usually contains constants for connection or autoloader settings
require_once('src/crest.php');
/**
* Function to download the report using a direct HTTP request,
* since the method vote.AttachedVote.download returns not JSON, but the content of the file.
*
* @param array $params - Parameters for the REST method (e.g., ['attachId' => 1])
*/
function downloadVoteReport(array $params, string $saveToFile): bool
{
// 1. Get authorization settings.
// CRest::getAppSettings() will return either data for OAuth or for webhook.
// In crest.php, change the access modifier to public for this method
$authData = CRest::getAppSettings();
if (empty($authData)) {
echo "Error: failed to get authorization settings. Check crest.php/settings.php.\n";
return false;
}
// 2. Define the URL for the request
if (!empty($authData['is_web_hook']) && $authData['is_web_hook'] === 'Y') {
// Case with webhook
$url = $authData['client_endpoint'] . 'vote.AttachedVote.download';
$queryParams = $params;
} else {
// Case with OAuth application
$url = $authData['client_endpoint'] . 'vote.AttachedVote.download';
$params['auth'] = $authData['access_token'];
$queryParams = $params;
}
$url .= '?' . http_build_query($queryParams);
echo "Request URL: " . $url . "\n";
// 3. Execute the request using cURL
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => $url,
CURLOPT_HEADER => false, // Do not include headers in the response
CURLOPT_RETURNTRANSFER => true, // Return the response as a string, not output to the browser
CURLOPT_USERAGENT => 'CRest based downloader',
CURLOPT_FOLLOWLOCATION => true, // Follow redirects
]);
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$error = curl_error($curl);
curl_close($curl);
// 4. Check the result and save the file
if ($error) {
echo "cURL error: " . $error . "\n";
return false;
}
if ($httpCode >= 400) {
echo "Server returned HTTP error " . $httpCode . ".\n";
// Attempt to decode the error if it's in JSON format
$errorData = json_decode($response, true);
if ($errorData && isset($errorData['error_description'])) {
echo "Error description: " . $errorData['error_description'] . "\n";
} else {
echo "Response body: " . $response . "\n";
}
return false;
}
// If all is well, save the response to a file
if (file_put_contents($saveToFile, $response)) {
echo "File successfully saved to: " . $saveToFile . "\n";
return true;
} else {
echo "Failed to save file to: " . $saveToFile . "\n";
return false;
}
}
// --- Example usage ---
$attachId = 1;
$fileType = 'xls';
$fileName = "vote_report_{$attachId}.{$fileType}";
$result = downloadVoteReport(
[
'attachId' => $attachId,
],
$fileName
);
if ($result) {
echo "Task completed.\n";
} else {
echo "Errors occurred during execution.\n";
}
Response Handling
HTTP status: 200 OK
In case of successful execution, the server returns not a JSON object, but the actual content of the file with HTTP headers that initiate the download in the browser Content-Disposition: attachment.
Error Handling
HTTP status: 4xx
{
"error": "ATTACH_NOT_FOUND",
"error_description": "Attach not found"
}
|
Name |
Description |
|
error |
String error code. It may consist of digits, Latin letters, and underscores |
|
error_description |
Textual description of the error. The description is not intended to be shown to the end user in its raw form |
Possible Error Codes
|
Code |
Description |
|
|
Vote not found |
|
|
No permission to participate in the vote |
Statuses and System Error Codes
HTTP Status: 20x, 40x, 50x
The errors described below may occur when calling any method.
|
Status |
Code |
Description |
|
|
|
An internal server error has occurred, please contact the server administrator or Bitrix24 technical support |
|
|
|
An internal server error has occurred, please contact the server administrator or Bitrix24 technical support |
|
|
|
The request intensity limit has been exceeded |
|
|
|
The current method is not allowed to be called using batch |
|
|
|
The maximum length of parameters passed to the batch method has been exceeded |
|
|
|
Invalid access token or webhook code |
|
|
|
The methods must be called using the HTTPS protocol |
|
|
|
The REST API is blocked due to overload. This is a manual individual block, to remove it you need to contact Bitrix24 technical support |
|
|
|
The REST API is available only on commercial plans |
|
|
|
The user whose access token or webhook was used to call the method lacks permissions |
|
|
|
The manifest is not available |
|
|
|
The request requires higher privileges than those provided by the webhook token |
|
|
|
The provided access token has expired |
|
|
|
The user does not have access to the application. This means that the application is installed, but the account administrator has allowed access to this application only for specific users |
|
|
|
The public part of the site is closed. To open the public part of the site on an on-premise installation, disable the option "Temporary closure of the public part of the site". Path to the setting: Desktop > Settings > Product Settings > Module Settings > Main Module > Temporary closure of the public part of the site |
Continue Learning
- Polls, Votes: Overview of Methods
- Get Data of Attached Vote vote.AttachedVote.get
- Get the list of users who voted for the answer vote.AttachedVote.getAnswerVoted
- Get Multiple Votes vote.AttachedVote.getMany
- Get Voting Data with Voter Information vote.AttachedVote.getWithVoted
- Recall Your Vote vote.AttachedVote.recall
- Resume Voting vote.AttachedVote.resume
- Stop voting vote.AttachedVote.stop
- Vote in the attached voting vote.AttachedVote.vote
- Create and Send a Vote in Chat vote.Integration.Im.send