Views: 2652
Last Modified: 18.01.2022

For convenient error debugging in AJAX life cycle, enable debug => true in .settings.php, then you will be able to view error and exception traces.

When required:

  • to pass the file, use the classes \Bitrix\Main\Engine\Response\File and \Bitrix\Main\Engine\Response\BFile:
    class Controller extends Engine\Controller
       {
        public function downloadAction($orderId)
         {
          //... find attached fileId by $orderId
          	return \Bitrix\Main\Engine\Response\BFile::createByFileId($fileId);
         }
        public function downloadGeneratedTemplateAction()
         {
           //... generate file ... $generatedPath
          	return new \Bitrix\Main\Engine\Response\File(
          	   $generatedPath, 
          	   'Test.pdf',
          	   \Bitrix\Main\Web\MimeType::getByFileExtension('pdf')
          	);
         }
    
        public function showImageAction($orderId)
         {
          	//... find attached imageId by $orderId
          	return \Bitrix\Main\Engine\Response\BFile::createByFileId($imageId)
          		->showInline(true)
          	;
          }
       }
    
  • to pass resized image, use \Bitrix\Main\Engine\Response\ResizedImage.

    Be advised, you cannot allow user to request arbitrary dimensions for resizing. Always sign the parameters or directly set dimensions inside code.
    class Controller extends Engine\Controller
       {
        public function showAvatarAction($userId)
          {
             //... find attached imageId by $userId
          	return \Bitrix\Main\Engine\Response\ResizedImage::createByImageId($imageId, 100, 100);
          }
       }    
    
  • to generate link in controller for the action from the same controller, use \Bitrix\Main\Engine\Controller::getActionUri
    public function getAction(File $file)
      {
        return [
           'file' => [
              'id' => $file->getId(),
       	  'name' => $file->getName(),
              'links' => [
                  'rename' => $this->getActionUri('rename', array('fileId' => $file->getId())),
                  ]				
             ]
         ];
      }    
    
    public function renameAction(File $file)
       {
          ...
       }
    
  • to generate link in controller for an action that will pass content, for example, to download file, then use \Bitrix\Main\Engine\Response\DataType\ContentUri. This is required for integration with REST module.
    public function getAction(File $file)
       {
          return [
             'file' => [
                'id' => $file->getId(),
                'name' => $file->getName(),
                'links' => [
                     'download' => new ContentUri($this->getActionUri('download', array('fileId' => $file->getId()))),
                             ]				
                        ]
                   ];
       }    
    
    public function downloadAction(File $file)
       {
         ...
       }
    
  • to convert data SNAKE_CASE as per standard to camelCase, then you can use auxiliary controller methods \Bitrix\Main\Engine\Controller::convertKeysToCamelCase, or direct config \Bitrix\Main\Engine\Response\Converter:
    public function getAction(File $file)
       {
          return [
              'file' => $this->convertKeysToCamelCase($fileData)
                   ];
       }    
    
    public function showInformationAction(File $file)
       {
          $converter = new \Bitrix\Main\Engine\Response\Converter(Converter::OUTPUT_JSON_FORMAT & ~Converter::RECURSIVE);
        		
          return $converter->process($data);
       }




Courses developed by Bitrix24